Compare length of two strings: Difference between revisions
m
no edit summary
RandomTask (talk | contribs) |
mNo edit summary |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 1,118:
</pre>
=={{header|C sharp|C#}}==
{{works with|C sharp|C#|10+}}
<syntaxhighlight lang="csharp">
void WriteSorted(string[] strings)
{
var sorted = strings.OrderByDescending(x => x.Length);
foreach(var s in sorted) Console.WriteLine($"{s.Length}: {s}");
var strings = new string[] { "abcd", "123456789", "abcdef", "1234567" };▼
}
</syntaxhighlight>
{{output}}
<pre>
9: 123456789
7: 1234567
6: abcdef
4: abcd
</pre>
Line 1,306 ⟶ 1,265:
scmp "Rosetta" "Code"
</syntaxhighlight>
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">
(defun sort-list-by-string-length (list-of-strings)
"Order LIST-OF-STRINGS from longest to shortest."
(sort list-of-strings 'longer-string)) ; sort by "longer-string" function below
(defun longer-string (string-1 string-2)
"Test if STRING-1 is longer than STRING-2."
(> (length string-1) (length string-2))) ; is STRING-1 longer than STRING-2?
</syntaxhighlight>
{{out}}
(sort-list-by-string-length '("abcd" "123456789" "abcdef" "1234567"))
<pre>
("123456789" "1234567" "abcdef" "abcd")
</pre>
=={{header|EMal}}==
|