Sort using a custom comparator: Difference between revisions

Content added Content deleted
(→‎{{header|Smalltalk}}: switched < for >)
m (Changed over to works with template)
Line 1: Line 1:
{{task}}Sort an array (or list) of strings in order of descending length, and in ascending lexicographic order for strings of equal length. Use a sorting facility provided by the language/library, combined with your own callback comparison function.
{{task}}

Sort an array (or list) of strings in order of descending length, and in ascending lexicographic order for strings of equal length. Use a sorting facility provided by the language/library, combined with your own callback comparison function.


'''Note:''' Lexicographic order is case-insensitive.
'''Note:''' Lexicographic order is case-insensitive.


=={{header|Ada}}==
=={{header|Ada}}==
'''Compiler:'''[[GNAT]] GPL 2006
{{works with|GNAT|GPL 2006}}
===Comparator_Package.ads===
===Comparator_Package.ads===
package Comparator_Package is
package Comparator_Package is
Line 105: Line 103:


=={{header|C}}==
=={{header|C}}==
'''Compiler:''' [[gcc]] 4.0.1
{{works with|gcc|4.0.1}}

'''Platform:''' [[BSD]]
'''Platform:''' [[BSD]]
#include <stdlib.h>
#include <stdlib.h>
Line 127: Line 124:


=={{header|C++}}==
=={{header|C++}}==
'''Compiler:''' [[g++]] 4.1.2
{{works with|g++|4.1.2}}

#include <algorithm>
#include <algorithm>
#include <string>
#include <string>
Line 183: Line 179:


=={{header|D}}==
=={{header|D}}==
'''Compiler:''' [[D]] - DMD 1.026
{{works with|D|DMD 1.026}}

{{libheader|Tango}}
{{libheader|Tango}}
<pre>module customsort ;
<pre>module customsort ;
Line 239: Line 234:


=={{header|Haskell}}==
=={{header|Haskell}}==
{{works with|GHC}}
'''Interpreter:''' [[GHC|GHCi]]
import List
import List
import Char
import Char
Line 274: Line 269:


=={{header|Java}}==
=={{header|Java}}==
{{works with|Java|1.5+}}
J2SE 5.0:

import java.util.Comparator;
import java.util.Comparator;
import java.util.Arrays;
import java.util.Arrays;
Line 336: Line 330:


=={{header|Perl}}==
=={{header|Perl}}==
{{works with|Perl|5.8.6}}
'''Interpreter:''' [[perl]] 5.8.6
sub mycmp { length $b <=> length $a or lc $a cmp lc $b }
sub mycmp { length $b <=> length $a or lc $a cmp lc $b }
Line 343: Line 337:


=={{header|PHP}}==
=={{header|PHP}}==
'''Interpreter:''' PHP 4.4.4 CLI
{{works with|PHP|4.4.4 CLI}}
<?php
<?php
function mycmp($s1, $s2)
function mycmp($s1, $s2)
Line 380: Line 374:


=={{header|Python}}==
=={{header|Python}}==
{{works with|Python|2.5}}
'''Interpreter''': 2.5
<pre>
<pre>
def mycmp(s1, s2):
def mycmp(s1, s2):