Compare length of two strings: Difference between revisions
Content added Content deleted
RandomTask (talk | contribs) |
RandomTask (talk | contribs) |
||
Line 1,174: | Line 1,174: | ||
"abcdef" has length 6 and is neither the longest nor the shortest string |
"abcdef" has length 6 and is neither the longest nor the shortest string |
||
"abcd" has length 4 and is the shortest string |
"abcd" has length 4 and is the shortest string |
||
</pre> |
|||
===Simple Modern Version=== |
|||
{{works with|C sharp|C#|12+}} |
|||
<syntaxhighlight lang="csharp"> |
|||
void WriteSorted(string[] strings) |
|||
{ |
|||
var sorted = strings.OrderByDescending(x => x.Length); |
|||
foreach(var s in sorted) Console.WriteLine($"{s.Length}: {s}"); |
|||
} |
|||
WriteSorted(new string[] { "abcd", "123456789", "abcdef", "1234567" }); |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
<pre> |
|||
9: 123456789 |
|||
7: 1234567 |
|||
6: abcdef |
|||
4: abcd |
|||
</pre> |
</pre> |
||