Compare length of two strings: Difference between revisions

Content added Content deleted
No edit summary
No edit summary
Line 1,223: Line 1,223:
"Rosetta" (length: 7)
"Rosetta" (length: 7)
"Code" (length: 4)
"Code" (length: 4)
</pre>

=={{header|EMal}}==
<syntaxhighlight lang="emal">
List list = text["abcd","123456789","abcdef","1234567", "مرحبا بالعالم"]
^|this solves the task doing the comparison by using the diamond operator|^
fun comparator = int by text a, text b do return b.length <> a.length end
List sorted = list.sort(comparator)
writeLine("text".padEnd(15, " ") + "chars".padStart(6, " ") + "bytes".padStart(6, " "))
for each text value in sorted
writeLine(value.padEnd(15, " ") +
(text!value.length).padStart(6, " ") +
^|conversion from text to blob uses utf8 encoding|^
(text!(blob!value).length).padStart(6, " "))
end
</syntaxhighlight>
{{out}}
<pre>
text chars bytes
مرحبا بالعالم 13 25
123456789 9 9
1234567 7 7
abcdef 6 6
abcd 4 4
</pre>
</pre>