Sort using a custom comparator: Difference between revisions

Frink
(Added solution for Action!)
(Frink)
Line 1,754:
be
to</pre>
 
=={{header|Frink}}==
The program statement is somewhat naive in saying "lexicographic order" as if it a single, well-defined thing. Lexicographic sorting rules and alphabetization rules vary widely from human language to human language and require a great deal of knowledge of those rules and of Unicode to perform correctly. Frink, however, has knowledge of alphabetization (collation) rules for a large number of human languages and will make you look smart. These are encapsulated in the <CODE>lexicalCompare</CODE> and <CODE>lexicalSort</CODE> functions. By default, these compare based on the language settings defined by your Java Virtual Machine (which should be those for your human language.) The following sorts Unicode correctly according to your human language's conventions. However, see below for a more flexible example that sorts for many of the world's languages!
<lang frink>f = {|a,b|
len = length[b] <=> length[a]
if len != 0
return len
else
return lexicalCompare[a,b]
}
 
words = split[%r/\s+/, "Here are some sample strings to be sorted"]
println[sort[words, f]]</lang>
{{out}}
<pre>
[strings, sample, sorted, Here, some, are, be, to]
</pre>
 
=={{header|FunL}}==
490

edits