Sort using a custom comparator: Difference between revisions

m
(added FunL)
m (→‎{{header|Wren}}: Minor tidy)
 
(140 intermediate revisions by 60 users not shown)
Line 1:
{{task|Sorting}}{{omit from|BBC BASICAlgorithms}}
[[Category:Sorting]]
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.
{{Sorting Algorithm}}
{{omit from|BBC BASIC}}
 
;Task:
'''Note:''' Lexicographic order is case-insensitive.
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.
=={{header|Ada}}==
 
 
'''Note:'''   Lexicographic order is case-insensitive.
<br><br>
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">V strings = ‘here are Some sample strings to be sorted’.split(‘ ’)
 
print(sorted(strings, key' x -> (-x.len, x.uppercase())))</syntaxhighlight>
 
{{out}}
<pre>
[strings, sample, sorted, here, Some, are, be, to]
</pre>
 
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program customSort64.s */
/* use merge sort iteratif and pointer table */
/* but use a extra table on stack for the merge */
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
 
/*******************************************/
/* Structures */
/********************************************/
/* city structure */
.struct 0
city_name: //
.struct city_name + 8 // string pointer
city_country: //
.struct city_country + 8 // string pointer
city_end:
/*********************************/
/* Initialized data */
/*********************************/
.data
sMessResult: .asciz "Name : @ country : @ \n"
szMessSortName: .asciz "Ascending sort table for name of city :\n"
szMessSortCitiesDesc: .asciz "Descending sort table for name of city : \n"
szCarriageReturn: .asciz "\n"
 
// cities name
szLondon: .asciz "London"
szNewyork: .asciz "New York"
szBirmin: .asciz "Birmingham"
szParis: .asciz "Paris"
// country name
szUK: .asciz "UK"
szUS: .asciz "US"
szFR: .asciz "FR"
.align 4
TableCities:
e1: .quad szLondon // address name string
.quad szUK // address country string
e2: .quad szParis
.quad szFR
e3: .quad szNewyork
.quad szUS
e4: .quad szBirmin
.quad szUK
e5: .quad szParis
.quad szUS
e6: .quad szBirmin
.quad szUS
/* pointers table */
ptrTableCities: .quad e1
.quad e2
.quad e3
.quad e4
.quad e5
.quad e6
.equ NBELEMENTS, (. - ptrTableCities) / 8
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
ldr x0,qAdrptrTableCities // address pointers table
bl displayTable
 
ldr x0,qAdrszMessSortName
bl affichageMess
 
ldr x0,qAdrptrTableCities // address pointers table
mov x1,0 // first element
mov x2,NBELEMENTS // number of élements
adr x3,comparAreaAlphaCrois // address custom comparator ascending
bl mergeSortIter
ldr x0,qAdrptrTableCities // address table
bl displayTable
 
ldr x0,qAdrszMessSortCitiesDesc
bl affichageMess
 
ldr x0,qAdrptrTableCities // address table
mov x1,0 // first element
mov x2,NBELEMENTS // number of élements
adr x3,comparAreaAlphaDecrois // address custom comparator descending
bl mergeSortIter
ldr x0,qAdrptrTableCities // address table
bl displayTable
100: // standard end of the program
mov x0,0 // return code
mov x8,EXIT // request to exit program
svc 0 // perform the system call
qAdrsZoneConv: .quad sZoneConv
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrsMessResult: .quad sMessResult
qAdrTableCities: .quad TableCities
qAdrszMessSortName: .quad szMessSortName
qAdrszMessSortCitiesDesc: .quad szMessSortCitiesDesc
qAdrptrTableCities: .quad ptrTableCities
 
/******************************************************************/
/* merge sort iteratif */
/* use an extra table on stack */
/******************************************************************/
/* x0 contains the address of table */
/* x1 contains the index of first element */
/* x2 contains the number of element */
/* x3 contains the address of custom comparator */
mergeSortIter:
stp fp,lr,[sp,-16]! // save registers
stp x1,x2,[sp,-16]! // save registers
stp x4,x5,[sp,-16]! // save registers
stp x6,x7,[sp,-16]! // save registers
stp x8,x9,[sp,-16]! // save registers
stp x10,x11,[sp,-16]! // save registers
stp x12,x13,[sp,-16]! // save registers
stp x14,x15,[sp,-16]! // save registers
mov x15,x0 // save address
mov x4,x1 // save N0 first element
sub x5,x2,1 // save N° last element
tst x2,1 // number of element odd ?
add x13,x2,1 // yes then add 1
csel x13,x13,x2,ne // to have a multiple to 16 bytes
lsl x13,x13,3 // for reserve the extra table to the stack
sub sp,sp,x13
mov fp,sp // frame register = address extra table on stack
mov x10,1 // subset size
1:
mov x6,x4 // first element
2:
lsl x8,x10,1 // compute end subset
add x8,x8,x6
sub x8,x8,1
add x7,x6,x8 // compute median
lsr x7,x7,1
cmp x8,x5 // maxi ?
ble 21f // no
mov x8,x5 // yes -> end subset = maxi
cmp x6,0 // subset final ?
beq 21f // no
cmp x7,x8 // compare median end subset
blt 21f
mov x7,x8 // maxi -> median
 
21:
add x9,x7,1
mov x0,x15
3: // copy first subset on extra table
sub x1,x9,1
ldr x2,[x0,x1,lsl 3]
str x2,[fp,x1,lsl 3]
sub x9,x9,1
cmp x9,x6
bgt 3b
mov x9,x7
cmp x7,x8
beq 41f
4: // and copy inverse second subset on extra table
add x1,x9,1
add x12,x7,x8
sub x12,x12,x9
ldr x2,[x0,x1,lsl 3]
str x2,[fp,x12,lsl 3]
add x9,x9,1
cmp x9,x8
blt 4b
41:
mov x11,x6 //k
mov x1,x6 // i
mov x2,x8 // j
5: // and now merge the two subset on final table
mov x0,fp
blr x3
cmp x0,0
bgt 7f
blt 6f
// si egalité et si i < pivot
cmp x1,x7
ble 6f
b 7f
6:
ldr x12,[fp,x1, lsl 3]
str x12,[x15,x11, lsl 3]
add x1,x1,1
b 8f
7:
ldr x12,[fp,x2, lsl 3]
str x12,[x15,x11, lsl 3]
sub x2,x2,1
8:
add x11,x11,1
cmp x11,x8
ble 5b
9:
mov x0,x15
lsl x2,x10,1
add x6,x6,x2 // compute new subset
cmp x6,x5 // end ?
ble 2b
lsl x10,x10,1 // size of subset * 2
cmp x10,x5 // end ?
ble 1b
100:
add sp,sp,x13 // stack alignement
ldp x14,x15,[sp],16 // restaur 2 registers
ldp x12,x13,[sp],16 // restaur 2 registers
ldp x10,x11,[sp],16 // restaur 2 registers
ldp x8,x9,[sp],16 // restaur 2 registers
ldp x6,x7,[sp],16 // restaur 2 registers
ldp x4,x5,[sp],16 // restaur 2 registers
ldp x1,x2,[sp],16 // restaur 2 registers
ldp fp,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/******************************************************************/
/* ascending comparison sort area */
/******************************************************************/
/* x0 contains the address of table */
/* x1 indice area sort 1 */
/* x2 indice area sort 2 */
comparAreaAlphaCrois:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
stp x4,x5,[sp,-16]! // save registers
stp x6,x7,[sp,-16]! // save registers
stp x8,x9,[sp,-16]! // save registers
ldr x1,[x0,x1,lsl 3] // load pointer element 1
ldr x6,[x1,city_name] // load area sort element 1
ldr x2,[x0,x2,lsl 3] // load pointer element 2
ldr x7,[x2,city_name] // load area sort element 2
 
mov x8,#0 // compar alpha string
1:
ldrb w9,[x6,x8] // byte string 1
ldrb w5,[x7,x8] // byte string 2
cmp w9,w5
bgt 11f // croissant
blt 10f
 
cmp w9,#0 // end string 1
beq 12f // end comparaison
add x8,x8,#1 // else add 1 in counter
b 1b // and loop
10: // lower
mov x0,-1
b 100f
11: // highter
mov x0,1
b 100f
12: // equal
mov x0,0
100:
ldp x8,x9,[sp],16 // restaur 2 registers
ldp x6,x7,[sp],16 // restaur 2 registers
ldp x4,x5,[sp],16 // restaur 2 registers
ldp x2,x3,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/******************************************************************/
/* descending comparison sort area */
/******************************************************************/
/* x0 contains the address of table */
/* x1 indice area sort 1 */
/* x2 indice area sort 2 */
comparAreaAlphaDecrois:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
stp x4,x5,[sp,-16]! // save registers
stp x6,x7,[sp,-16]! // save registers
stp x8,x9,[sp,-16]! // save registers
ldr x1,[x0,x1,lsl 3] // load pointer element 1
ldr x6,[x1,city_name] // load area sort element 1
ldr x2,[x0,x2,lsl 3] // load pointer element 2
ldr x7,[x2,city_name] // load area sort element 2
 
mov x8,#0 // compar alpha string
1:
ldrb w9,[x6,x8] // byte string 1
ldrb w5,[x7,x8] // byte string 2
cmp w9,w5
blt 11f // descending
bgt 10f
 
cmp w9,#0 // end string 1
beq 12f // end comparaison
add x8,x8,#1 // else add 1 in counter
b 1b // and loop
10: // lower
mov x0,-1
b 100f
11: // highter
mov x0,1
b 100f
12: // equal
mov x0,0
100:
ldp x8,x9,[sp],16 // restaur 2 registers
ldp x6,x7,[sp],16 // restaur 2 registers
ldp x4,x5,[sp],16 // restaur 2 registers
ldp x2,x3,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/******************************************************************/
/* Display table elements */
/******************************************************************/
/* x0 contains the address of table */
displayTable:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
stp x4,x5,[sp,-16]! // save registers
stp x6,x7,[sp,-16]! // save registers
mov x2,x0 // table address
mov x3,0
1: // loop display table
lsl x4,x3,#3 // offset element
ldr x6,[x2,x4] // load pointer
ldr x1,[x6,city_name]
ldr x0,qAdrsMessResult
bl strInsertAtCharInc // put name in message
ldr x1,[x6,city_country] // and put country in the message
bl strInsertAtCharInc // insert result at @ character
bl affichageMess // display message
add x3,x3,1
cmp x3,#NBELEMENTS
blt 1b
ldr x0,qAdrszCarriageReturn
bl affichageMess
100:
ldp x6,x7,[sp],16 // restaur 2 registers
ldp x4,x5,[sp],16 // restaur 2 registers
ldp x2,x3,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
<pre>
Name : London country : UK
Name : Paris country : FR
Name : New York country : US
Name : Birmingham country : UK
Name : Paris country : US
Name : Birmingham country : US
 
Ascending sort table for name of city :
Name : Birmingham country : UK
Name : Birmingham country : US
Name : London country : UK
Name : New York country : US
Name : Paris country : FR
Name : Paris country : US
 
Descending sort table for name of city :
Name : Paris country : FR
Name : Paris country : US
Name : New York country : US
Name : London country : UK
Name : Birmingham country : UK
Name : Birmingham country : US
</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">DEFINE PTR="CARD"
 
PROC PrintArray(PTR ARRAY a INT size)
INT i
 
Put('[)
FOR i=0 TO size-1
DO
IF i>0 THEN Put(' ) FI
Print(a(i))
OD
Put(']) PutE()
RETURN
 
INT FUNC CustomComparator(CHAR ARRAY s1,s2)
INT res
 
res=s2(0) res==-s1(0)
IF res=0 THEN
res=SCompare(s1,s2)
FI
RETURN (res)
 
INT FUNC Comparator=*(CHAR ARRAY s1,s2)
DEFINE JSR="$20"
DEFINE RTS="$60"
[JSR $00 $00 ;JSR to address set by SetComparator
RTS]
 
PROC SetComparator(PTR p)
PTR addr
 
addr=Comparator+1 ;location of address of JSR
PokeC(addr,p)
RETURN
 
PROC InsertionSort(PTR ARRAY a INT size PTR compareFun)
INT i,j
CHAR ARRAY s
 
SetComparator(compareFun)
FOR i=1 TO size-1
DO
s=a(i)
j=i-1
WHILE j>=0 AND Comparator(s,a(j))<0
DO
a(j+1)=a(j)
j==-1
OD
a(j+1)=s
OD
RETURN
 
PROC Test(PTR ARRAY a INT size PTR compareFun)
PrintE("Array before sort:")
PrintArray(a,size)
PutE()
 
InsertionSort(a,size,compareFun)
PrintE("Array after sort:")
PrintArray(a,size)
PutE()
RETURN
 
PROC Main()
PTR ARRAY a(24)
 
a(0)="lorem" a(1)="ipsum" a(2)="dolor" a(3)="sit"
a(4)="amet" a(5)="consectetur" a(6)="adipiscing"
a(7)="elit" a(8)="maecenas" a(9)="varius"
a(10)="sapien" a(11)="vel" a(12)="purus"
a(13)="hendrerit" a(14)="vehicula" a(15)="integer"
a(16)="hendrerit" a(17)="viverra" a(18)="turpis" a(19)="ac"
a(20)="sagittis" a(21)="arcu" a(22)="pharetra" a(23)="id"
Test(a,24,CustomComparator)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sort_using_a_custom_comparator.png Screenshot from Atari 8-bit computer]
<pre>
Array before sort:
[lorem ipsum dolor sit amet consectetur adipiscing elit
maecenas varius sapien vel purus hendrerit vehicula integer
hendrerit viverra turpis ac sagittis arcu pharetra id]
 
Array after sort:
[consectetur adipiscing hendrerit hendrerit maecenas
pharetra sagittis vehicula integer viverra sapien turpis varius
dolor ipsum lorem purus amet arcu elit sit vel ac id]
</pre>
 
=={{header|Ada}}==
{{incorrect}}
{{works with|GNAT|}}
<langsyntaxhighlight lang="ada">
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
Line 115 ⟶ 614:
Put_Line("Sorted list:");
Put(Strings);
end Custom_Compare;</langsyntaxhighlight>
{{out}}
===Output===
<pre>Unsorted list:
The output file looks like this:
<lang txt>Unsorted list:
this
is
Line 152 ⟶ 650:
To
a
A</langpre>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
The Algol 68 version of the Quicksort algorithm, modified to use a custom sort routine, as per this task.
<syntaxhighlight lang="algol68"># define the MODE that will be sorted #
MODE SITEM = STRING;
#--- Swap function ---#
PROC swap = (REF[]SITEM array, INT first, INT second) VOID:
(
SITEM temp := array[first];
array[first] := array[second];
array[second]:= temp
);
#--- Quick sort partition arg function with custom comparision function ---#
PROC quick = (REF[]SITEM array, INT first, INT last, PROC(SITEM,SITEM)INT compare) VOID:
(
INT smaller := first + 1,
larger := last;
SITEM pivot := array[first];
WHILE smaller <= larger DO
WHILE compare(array[smaller], pivot) < 0 AND smaller < last DO
smaller +:= 1
OD;
WHILE compare( array[larger], pivot) > 0 AND larger > first DO
larger -:= 1
OD;
IF smaller < larger THEN
swap(array, smaller, larger);
smaller +:= 1;
larger -:= 1
ELSE
smaller +:= 1
FI
OD;
swap(array, first, larger);
IF first < larger-1 THEN
quick(array, first, larger-1, compare)
FI;
IF last > larger +1 THEN
quick(array, larger+1, last, compare)
FI
);
#--- Quick sort array function with custom comparison function ---#
PROC quicksort = (REF[]SITEM array, PROC(SITEM,SITEM)INT compare) VOID:
(
IF UPB array > LWB array THEN
quick(array, LWB array, UPB array, compare)
FI
);
#***************************************************************#
main:
(
OP LENGTH = (STRING a)INT: ( UPB a + 1 ) - LWB a;
OP TOLOWER = (STRING a)STRING:
BEGIN
STRING result := a;
FOR i FROM LWB result TO UPB result DO
CHAR c = a[i];
IF c >= "A" AND c <= "Z" THEN result[i] := REPR ( ( ABS c - ABS "A" ) + ABS "a" ) FI
OD;
result
END # TOLOWER # ;
# custom comparison, descending sort on length #
# if lengths are equal, sort lexicographically #
PROC compare = (SITEM a, b)INT:
IF INT a length = LENGTH a;
INT b length = LENGTH b;
a length < b length
THEN
# a is shorter than b # 1
ELIF a length > b length
THEN
# a is longer than b # -1
ELIF STRING lower a = TOLOWER a;
STRING lower b = TOLOWER b;
lower a < lower b
THEN
# lowercase a is before lowercase b # -1
ELIF lower a > lower b
THEN
# lowercase a is after lowercase b # 1
ELIF a > b
THEN
# a and b are equal ignoring case, #
# but a is after b considering case # 1
ELIF a < b
THEN
# a and b are equal ignoring case, #
# but a is before b considering case # -1
ELSE
# the strings are equal # 0
FI # compare # ;
[]SITEM orig = ("Here", "are", "some", "sample", "strings", "to", "be", "sorted");
[LWB orig : UPB orig]SITEM a := orig;
print(("Before:"));FOR i FROM LWB a TO UPB a DO print((" ",a[i])) OD; print((newline));
quicksort(a, compare);
print(("After :"));FOR i FROM LWB a TO UPB a DO print((" ",a[i])) OD; print((newline))
)</syntaxhighlight>
{{out}}
<pre>
Before: Here are some sample strings to be sorted
After : strings sample sorted Here some are be to
</pre>
 
=={{header|AppleScript}}==
===ASObjC using records===
AppleScript is not itself well equipped with sorting functions, but from Yosemite onwards we can make some use of ObjC classes. While a classic comparator function can not readily be passed from AppleScript to ObjC, we can at least write a custom function which lifts atomic values into records (with keys to base and derivative values), and also passes a sequence of (key, bool) pairs, where the bool expresses the choice between ascending and descending order for the paired key:
 
<syntaxhighlight lang="applescript">use framework "Foundation"
 
-- SORTING LISTS OF ATOMIC (NON-RECORD) DATA WITH A CUSTOM SORT FUNCTION
 
-- In sortBy, f is a function from () to a tuple of two parts:
-- 1. a function from any value to a record derived from (and containing) that value
-- The base value should be present in the record under the key 'value'
-- additional derivative keys (and optionally the 'value' key) should be included in 2:
-- 2. a list of (record key, boolean) tuples, in the order of sort comparison,
-- where the value *true* selects ascending order for the paired key
-- and the value *false* selects descending order for that key
 
-- sortBy :: (() -> ((a -> Record), [(String, Bool)])) -> [a] -> [a]
on sortBy(f, xs)
set {fn, keyBools} to mReturn(f)'s |λ|()
script unWrap
on |λ|(x)
value of x
end |λ|
end script
map(unWrap, sortByComparing(keyBools, map(fn, xs)))
end sortBy
 
-- SORTING APPLESCRIPT RECORDS BY PRIMARY AND N-ARY SORT KEYS
 
-- sortByComparing :: [(String, Bool)] -> [Records] -> [Records]
on sortByComparing(keyDirections, xs)
set ca to current application
script recDict
on |λ|(x)
ca's NSDictionary's dictionaryWithDictionary:x
end |λ|
end script
set dcts to map(recDict, xs)
script asDescriptor
on |λ|(kd)
set {k, d} to kd
ca's NSSortDescriptor's sortDescriptorWithKey:k ascending:d selector:dcts
end |λ|
end script
((ca's NSArray's arrayWithArray:dcts)'s ¬
sortedArrayUsingDescriptors:map(asDescriptor, keyDirections)) as list
end sortByComparing
 
 
-- GENERIC FUNCTIONS ---------------------------------------------------------
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
 
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn
 
 
-- TEST ----------------------------------------------------------------------
on run
set xs to ["Shanghai", "Karachi", "Beijing", "Sao Paulo", "Dhaka", "Delhi", "Lagos"]
-- Custom comparator:
-- Returns a lifting function and a sequence of {key, bool} pairs
-- Strings in order of descending length,
-- and ascending lexicographic order
script lengthDownAZup
on |λ|()
script
on |λ|(x)
{value:x, n:length of x}
end |λ|
end script
{result, {{"n", false}, {"value", true}}}
end |λ|
end script
sortBy(lengthDownAZup, xs)
end run</syntaxhighlight>
{{Out}}
<pre>{"Sao Paulo", "Shanghai", "Beijing", "Karachi", "Delhi", "Dhaka", "Lagos"}</pre>
 
===ASObjC without records===
 
Putting values into records temporarily can sometimes be necessary with ASObjC sorts so that sorting can be done on the equivalent NSDictionaries' keys. But in fact NSStrings can be sorted on the keys <tt>"length"</tt> and <tt>"self"</tt>:
 
<syntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
 
set listOfText to words of "now is the time for all good men to come to the aid of the party"
 
set arrayOfStrings to current application's class "NSMutableArray"'s arrayWithArray:(listOfText)
set descendingByLength to current application's class "NSSortDescriptor"'s sortDescriptorWithKey:("length") ascending:(false)
set ascendingLexicographically to current application's class "NSSortDescriptor"'s sortDescriptorWithKey:("self") ascending:(true) selector:("localizedStandardCompare:")
tell arrayOfStrings to sortUsingDescriptors:({descendingByLength, ascendingLexicographically})
 
return arrayOfStrings as list</syntaxhighlight>
 
{{output}}
<pre>{"party", "come", "good", "time", "aid", "all", "for", "men", "now", "the", "the", "the", "is", "of", "to", "to"}</pre>
 
===Vanilla===
<syntaxhighlight lang="applescript">use AppleScript version "2.3.1" -- OS X 10.9 (Mavericks) or later
use sorter : script ¬
"Custom Iterative Ternary Merge Sort" --<www.macscripter.net/t/timsort-and-nigsort/71383/3>
 
-- Sort customiser.
script descendingByLengthThenAscendingLexicographically
on isGreater(a, b)
set lenA to a's length
set lenB to b's length
if (lenA = lenB) then return (a > b)
return (lenB > lenA)
end isGreater
end script
 
set listOfText to words of "now is the time for all good men to come to the aid of the party"
tell sorter to ¬
sort(listOfText, 1, -1, {comparer:descendingByLengthThenAscendingLexicographically})
return listOfText</syntaxhighlight>
 
{{output}}
<pre>{"party", "come", "good", "time", "aid", "all", "for", "men", "now", "the", "the", "the", "is", "of", "to", "to"}</pre>
 
=={{header|ATS}}==
 
<syntaxhighlight lang="ats">(* The following demonstrates a few ways to customize the
comparator. *)
 
#include "share/atspre_staload.hats"
staload UN = "prelude/SATS/unsafe.sats"
 
%{^
#include <strings.h>
%}
 
extern fn
strcasecmp : (string, string) -<> int = "mac#strcasecmp"
 
fn
sort_strings_1 (lst : List string,
cmp : (string, string) -<> int)
:<!wrt> List string =
list_vt2t (list_mergesort_fun<string> (lst, cmp))
 
fn
sort_strings_2 (lst : List string,
cmp : (string, string) -<cloref> int)
:<!wrt> List string =
list_vt2t (list_mergesort_cloref<string> (lst, cmp))
 
fn
sort_using_a_template_function (lst : List string)
:<!wrt> List string =
(* There is no actual callback here. The comparison code is expanded
directly into the sort code. *)
let
implement
list_mergesort$cmp<string> (x, y) =
let
val m = length x
and n = length y
in
if m < n then
1
else if n < m then
~1
else
strcasecmp (x, y)
end
in
(* The list mergesort template functions in the ATS prelude return
_linear_ lists. Thus the call to list_vt2t to cast that result
to an ordinary list. *)
list_vt2t (list_mergesort<string> lst)
end
 
fn
sort_using_an_ordinary_function (lst : List string)
:<!wrt> List string =
(* Rather than expand the comparison code, incorporate a function
call into the sort implementation. *)
let
fn
cmp (x : string,
y : string)
:<> int =
let
val m = length x
and n = length y
in
if m < n then
1
else if n < m then
~1
else
strcasecmp (x, y)
end
in
list_vt2t (list_mergesort_fun<string> (lst, cmp))
end
 
fn
sort_the_way_it_works_for_qsort_in_C (lst : List string)
:<!wrt> List string =
(* Here we have a true callback to an ordinary function. *)
let
fn
cmp (x : string,
y : string)
:<> int =
let
val m = length x
and n = length y
in
if m < n then
1
else if n < m then
~1
else
strcasecmp (x, y)
end
in
sort_strings_1 (lst, cmp)
end
 
fn
sort_using_a_closure (lst : List string)
:<!wrt> List string =
(* Incorporate a closure into the sort implementation. (Standard C
does not have closures.) *)
let
fn
cmp (x : string,
y : string)
:<cloref> int =
let
val m = length x
and n = length y
in
if m < n then
1
else if n < m then
~1
else
strcasecmp (x, y)
end
in
list_vt2t (list_mergesort_cloref<string> (lst, cmp))
end
 
fn
sort_by_calling_back_to_a_closure (lst : List string)
:<!wrt> List string =
let
fn
cmp (x : string,
y : string)
:<cloref> int =
let
val m = length x
and n = length y
in
if m < n then
1
else if n < m then
~1
else
strcasecmp (x, y)
end
in
sort_strings_2 (lst, cmp)
end
 
implement
main0 () =
let
val unsorted =
$list{string}
("Here", "are", "some", "sample", "strings",
"to", "be", "sorted")
 
val sorted1 = sort_using_a_template_function unsorted
val sorted2 = sort_using_an_ordinary_function unsorted
val sorted3 = sort_the_way_it_works_for_qsort_in_C unsorted
val sorted4 = sort_using_a_closure unsorted
val sorted5 = sort_by_calling_back_to_a_closure unsorted
in
println! unsorted;
println! sorted1;
println! sorted2;
println! sorted3;
println! sorted4;
println! sorted5
end</syntaxhighlight>
 
{{out}}
<pre>$ patscc -DATS_MEMALLOC_GCBDW -O3 sort_using_custom_comparator.dats -lgc && ./a.out
Here, are, some, sample, strings, to, be, sorted
strings, sample, sorted, Here, some, are, be, to
strings, sample, sorted, Here, some, are, be, to
strings, sample, sorted, Here, some, are, be, to
strings, sample, sorted, Here, some, are, be, to
strings, sample, sorted, Here, some, are, be, to</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">numbers = 5,3,7,9,1,13,999,-4
strings = Here,are,some,sample,strings,to,be,sorted
Sort, numbers, F IntegerSort D,
Line 168 ⟶ 1,096:
StringLengthSort(a1, a2){
return strlen(a1) - strlen(a2)
}</langsyntaxhighlight>
 
=={{header|AWK}}==
For GAWK, this uses the inbuilt descending numeric ordering and a custom comparison routine for caseless string comparison.
<lang AWK>
May need modification for TAWK.
# syntax: GAWK -f SORT_USING_A_CUSTOM_COMPARATOR.AWK
<syntaxhighlight lang="awk"># syntax: GAWK -f SORT_USING_A_CUSTOM_COMPARATOR.AWK
#
# sorting:
Line 189 ⟶ 1,119:
PROCINFO["sorted_in"] = "@ind_num_desc" ; SORTTYPE = 9
for (i in arr) {
PROCINFO["sorted_in"] = "@ind_str_asccaselessCompare" ; SORTTYPE = 2 # possibly 14?
for (j in arr[i]) {
for (k=1; k<=arr[i][j]; k++) {
Line 198 ⟶ 1,128:
exit(0)
}
function caselessCompare( i1, v1, i2, v2, l1, l2, result )
</lang>
{
<p>output:</p>
l1 = tolower( i1 );
l2 = tolower( i2 );
return ( ( l1 < l2 ) ? -1 : ( ( l1 == l2 ) ? 0 : 1 ) );
} # caselessCompare</syntaxhighlight>
{{out}}
<pre>
unsorted:
Line 226 ⟶ 1,161:
Strings
strings
sort
Sort
This
sort
this
Set
set
Set
Is
Of
To
is
Is
of
Of
to
To
A
a
A
</pre>
 
=={{header|Babel}}==
 
To sort ASCII strings, use the strsort or lexsort utilities to sort alphabetically and lexicographically, respectively.
 
<syntaxhighlight lang="babel">babel> ("Here" "are" "some" "sample" "strings" "to" "be" "sorted") strsort ! lsstr !
( "Here" "are" "be" "sample" "some" "sorted" "strings" "to" )
babel> ("Here" "are" "some" "sample" "strings" "to" "be" "sorted") lexsort ! lsstr !
( "be" "to" "are" "Here" "some" "sample" "sorted" "strings" )</syntaxhighlight>
 
If you want to sort UTF-8 encoded Unicode strings, first convert to array-string form using the str2ar operator, then sort using the strcmp operator. To sort lexicographically, use the arcmp operator. The following examples illustrate each case:
 
<syntaxhighlight lang="babel">babel> ("Here" "are" "some" "sample" "strings" "to" "be" "sorted") {str2ar} over ! {strcmp 0 lt?} lssort ! {ar2str} over ! lsstr !
( "Here" "are" "be" "some" "sample" "sorted" "strings" "to" )
babel> ("Here" "are" "some" "sample" "strings" "to" "be" "sorted") {str2ar} over ! {arcmp 0 lt?} lssort ! {ar2str} over ! lsstr !
( "be" "to" "are" "Here" "some" "sample" "sorted" "strings" )</syntaxhighlight>
 
You can sort a list of any kind of structure you like using the lssort utility. Use the lt? numerical comparison operator for sorting numerical lists:
 
<syntaxhighlight lang="babel">babel> ( 5 6 8 4 5 3 9 9 4 9 ) {lt?} lssort ! lsnum !
( 3 4 4 5 5 6 8 9 9 9 )</syntaxhighlight>
 
You can even shuffle a list with lssort using the randlf operator (your results will probably differ):
 
<syntaxhighlight lang="babel">babel> (1 2 3 4 5 6 7 8 9) {1 randlf 2 rem} lssort ! lsnum !
( 7 5 9 6 2 4 3 1 8 )</syntaxhighlight>
 
To sort complex objects, you need to access the relevant field in each object, and then provide the result of comparing them. For example, to sort a list of pairs by first number:
 
<syntaxhighlight lang="babel">
babel> 20 lsrange ! {1 randlf 2 rem} lssort ! 2 group ! --> this creates a shuffled list of pairs
babel> dup {lsnum !} ... --> display the shuffled list, pair-by-pair
( 11 10 )
( 15 13 )
( 12 16 )
( 17 3 )
( 14 5 )
( 4 19 )
( 18 9 )
( 1 7 )
( 8 6 )
( 0 2 )
babel> {<- car -> car lt? } lssort ! --> sort the list by first element of each pair
babel> dup {lsnum !} ... --> display the sorted list, pair-by-pair
( 0 2 )
( 1 7 )
( 4 19 )
( 8 6 )
( 11 10 )
( 12 16 )
( 14 5 )
( 15 13 )
( 17 3 )
( 18 9 )</syntaxhighlight>
 
=={{header|Burlesque}}==
 
<langsyntaxhighlight lang="burlesque">
blsq ) {"acb" "Abc" "Acb" "acc" "ADD"}><
{"ADD" "Abc" "Acb" "acb" "acc"}
blsq ) {"acb" "Abc" "Acb" "acc" "ADD"}(zz)CMsb
{"Abc" "acb" "Acb" "acc" "ADD"}
</syntaxhighlight>
</lang>
 
=={{header|C}}==
{{works with|POSIX|.1-2001}}
 
<langsyntaxhighlight lang="c">#include <stdlib.h> /* for qsort */
#include <string.h> /* for strlen */
#include <strings.h> /* for strcasecmp */
Line 275 ⟶ 1,264:
qsort(strings, sizeof(strings)/sizeof(*strings), sizeof(*strings), mycmp);
return 0;
}</langsyntaxhighlight>
 
=={{header|C++ sharp|C#}}==
{{works with|g++|4.1.2}}
<lang cpp>#include <algorithm>
#include <string>
#include <cctype>
 
{{incorrect}}
// compare character case-insensitive
Wrong compare. Because can't find "a" < "A"
struct icompare_char {
bool operator()(char c1, char c2) {
return std::toupper(c1) < std::toupper(c2);
}
};
 
// return true if s1 comes before s2
struct compare {
bool operator()(std::string const& s1, std::string const& s2) {
if (s1.length() > s2.length())
return true;
if (s1.length() < s2.length())
return false;
return std::lexicographical_compare(s1.begin(), s1.end(),
s2.begin(), s2.end(),
icompare_char());
}
};
 
int main() {
std::string strings[8] = {"Here", "are", "some", "sample", "strings", "to", "be", "sorted"};
std::sort(strings, strings+8, compare());
return 0;
}</lang>
 
=={{header|C sharp|C#}}==
C# allows you to specify a custom compare to the built in sort method on a list
 
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
 
Line 351 ⟶ 1,313:
}
}
}</langsyntaxhighlight>
 
{{out}}
===Output File===
<pre>Unsorted
********
Line 389 ⟶ 1,351:
 
=== Alternative using Linq (.NET 3.5) ===
{{incorrect}}
<lang csharp>using System;
Has not the case of equal in lower case and then make them in order using the exact character case, so "a" comes before "A"
 
 
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 428 ⟶ 1,394:
}
}
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
{{works with|g++|4.1.2}}
<syntaxhighlight lang="cpp">#include <algorithm>
#include <string>
#include <cctype>
 
// compare character case-insensitive
struct icompare_char {
bool operator()(char c1, char c2) {
return std::toupper(c1) < std::toupper(c2);
}
};
 
// return true if s1 comes before s2
struct compare {
bool operator()(std::string const& s1, std::string const& s2) {
if (s1.length() > s2.length())
return true;
if (s1.length() < s2.length())
return false;
return std::lexicographical_compare(s1.begin(), s1.end(),
s2.begin(), s2.end(),
icompare_char());
}
};
 
int main() {
std::string strings[8] = {"Here", "are", "some", "sample", "strings", "to", "be", "sorted"};
std::sort(strings, strings+8, compare());
return 0;
}</syntaxhighlight>
 
=={{header|Ceylon}}==
<syntaxhighlight lang="ceylon">shared void run() {
 
value strings = [
"Cat", "apple", "Adam", "zero", "Xmas", "quit",
"Level", "add", "Actor", "base", "butter"
];
value sorted = strings.sort((String x, String y) =>
if(x.size == y.size)
then increasing(x.lowercased, y.lowercased)
else decreasing(x.size, y.size));
sorted.each(print);
}</syntaxhighlight>
 
=={{header|Clean}}==
<langsyntaxhighlight lang="clean">import StdEnv
 
less s1 s2
Line 441 ⟶ 1,455:
lower s = {toLower c \\ c <-: s}
 
Start = sortBy less ["This", "is", "a", "set", "of", "strings", "to", "sort"]</langsyntaxhighlight>
 
=={{header|Clojure}}==
Clojure's ''sort'' function has a 2-argument version where the first argument is a ''java.util.Comparator'', and the second is the collection to be sorted. Thus the heart of this version is a comparator function that satisfies the problem spec. What makes this work is that all Clojure functions (thus ''rosetta-code'' defined here) implement the ''java.util.Comparator'' interface.
<langsyntaxhighlight lang="clojure">(defn rosetta-compare [s1 s2]
(let [len1 (count s1), len2 (count s2)]
(if (= len1 len2)
Line 453 ⟶ 1,467:
(println
(sort rosetta-compare
["Here" "are" "some" "sample" "strings" "to" "be" "sorted"]))</langsyntaxhighlight>
 
{{out}}
Output:
<pre>
(strings sample sorted Here some are be to)
Line 461 ⟶ 1,475:
 
An alternative, using <tt>sort-by</tt>:
<langsyntaxhighlight lang="clojure">(sort-by (juxt (comp - count) #(.toLowerCase %))
["Here" "are" "some" "sample" "strings" "to" "be" "sorted"])</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
In Common Lisp, the sort function takes a "less than" predicate that is used as the comparator. This parameter can be any two-argument function. Note: Common Lisp's <tt>sort</tt> function is destructive; for lists you should not use the original list afterwards, you should only use the return value. This also means you don't call it directly on constant data.
 
For example, to sort strings case-insensitively in ascending order:
 
<langsyntaxhighlight lang="lisp">CL-USER> (defvar *strings*
'(list "Cat" "apple" "Adam" "zero" "Xmas" "quit" "Level" "add" "Actor" "base" "butter"))
*STRINGS*
CL-USER> (sort *strings* #'string-lessp)
("Actor" "Adam" "add" "apple" "base" "butter" "Cat" "Level" "quit" "Xmas"
"zero")</langsyntaxhighlight>
 
You can also provide an optional key function which maps each element to a key. The keys are then compared using the comparator. For example, to sort strings by length in descending order:
 
<langsyntaxhighlight lang="lisp">CL-USER> (defvar *strings*
'(list "Cat" "apple" "Adam" "zero" "Xmas" "quit" "Level" "add" "Actor" "base" "butter"))
*STRINGS*
CL-USER> (sort *strings* #'> :key #'length)
("butter" "apple" "Level" "Actor" "Adam" "zero" "Xmas" "quit" "base"
"Cat" "add")</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.string, std.algorithm, std.typecons;
 
void main() {
Line 493 ⟶ 1,507:
.schwartzSort!q{ tuple(-a.length, a.toUpper) }
.writeln;
}</langsyntaxhighlight>
{{out}}
<pre>["strings", "sample", "sorted", "here", "Some", "are", "be", "to"]</pre>
 
===Alternative Version===
The more natural and efficient way to solve this problem is to use <code>std.algorith.multiSort</code>. But currently it's less convenient because it can't be used with the UFCSyntax (same output):
But currently it's less convenient because it can't be used with the UFCSyntax (same output):
<lang d>void main() {
<syntaxhighlight lang="d">void main() {
import std.stdio, std.string, std.algorithm;
 
auto parts = "here are Some sample strings to be sorted".split;
parts.multiSort!(q{a.length > b.length}, q{a.toUpper < b.toUpper});
 
parts.multiSort!("a.length > b.length", "a.toUpper < b.toUpper");
 
parts.writeln;
}</langsyntaxhighlight>
 
=={{header|Delphi}}==
<langsyntaxhighlight Delphilang="delphi">program SortWithCustomComparator;
 
{$APPTYPE CONSOLE}
Line 524 ⟶ 1,537:
function(const Left, Right: string): Integer
begin
//Returns ('Here', 'are', 'be', 'sample', 'some', 'sorted', 'strings', 'to')
Result := Length(Right) - Length(Left);
//Result := CompareStr(Left, Right);
 
//Returns ('are', 'be', 'Here', 'sample', 'some', 'sorted', 'strings', 'to')
Result := CompareText(Left, Right);
end));
end.</langsyntaxhighlight>
 
=={{header|E}}==
<langsyntaxhighlight lang="e">/** returns a if it is nonzero, otherwise b() */
def nonzeroOr(a, b) { return if (a.isZero()) { b() } else { a } }
 
Line 536 ⟶ 1,553:
nonzeroOr(b.size().op__cmp(a.size()),
fn { a.compareToIgnoreCase(b) })
})</langsyntaxhighlight>
 
=={{header|EGL}}==
 
{{works with|EDT|}}
<syntaxhighlight lang="egl">program SortExample
function main()
test1 string[] = ["Here", "are", "some", "sample", "strings", "to", "be", "sorted"];
test1.sort(sortFunction);
 
SysLib.writeStdout("Test 1:");
for(i int from 1 to test1.getSize())
SysLib.writeStdout(test1[i]);
end
 
test2 string[] = ["Cat", "apple", "Adam", "zero", "Xmas", "quit", "Level", "add", "Actor", "base", "butter"];
test2.sort(sortFunction);
SysLib.writeStdout("Test 2:");
for(i int from 1 to test2.getSize())
SysLib.writeStdout(test2[i]);
end
end
function sortFunction(a any in, b any in) returns (int)
result int = (b as string).length() - (a as string).length();
if (result == 0)
case
when ((a as string).toLowerCase() > (b as string).toLowerCase())
result = 1;
when ((a as string).toLowerCase() < (b as string).toLowerCase())
result = -1;
otherwise
result = 0;
end
end
return result;
end
end</syntaxhighlight>
 
{{out}}
<pre>Test 1:
strings
sample
sorted
Here
some
are
be
to
 
Test 2:
butter
Actor
apple
Level
Adam
base
quit
Xmas
zero
add
Cat</pre>
 
=={{header|Elena}}==
ELENA 6.x :
<syntaxhighlight lang="elena">import extensions;
import system'routines;
import system'culture;
public program()
{
var items := new string[]{ "Here", "are", "some", "sample", "strings", "to", "be", "sorted" };
console.printLine("Unsorted: ", items.asEnumerable());
console.printLine("Descending length: ", items.clone()
.sort::(p,n => p.Length > n.Length).asEnumerable());
console.printLine("Ascending order: ", items.clone()
.sort::(p,n => p.toUpper(invariantLocale) < n.toUpper(invariantLocale)).asEnumerable())
}</syntaxhighlight>
{{out}}
<pre>
Unsorted: Here,are,some,sample,strings,to,be,sorted
Descending length: strings,sorted,sample,some,Here,are,be,to
Ascending order: are,be,Here,sample,some,sorted,strings,to
</pre>
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">strs = ~w[this is a set of strings to sort This Is A Set Of Strings To Sort]
 
comparator = fn s1,s2 -> if String.length(s1)==String.length(s2),
do: String.downcase(s1) <= String.downcase(s2),
else: String.length(s1) >= String.length(s2) end
IO.inspect Enum.sort(strs, comparator)
 
# or
IO.inspect Enum.sort_by(strs, fn str -> {-String.length(str), String.downcase(str)} end)</syntaxhighlight>
 
{{out}}
<pre>
["strings", "Strings", "sort", "Sort", "this", "This", "set", "Set", "is", "Is",
"of", "Of", "to", "To", "a", "A"]
</pre>
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( sort_using_custom_comparator ).
 
Line 552 ⟶ 1,676:
longest_first_case_insensitive( String1, String2 ) when erlang:length(String1) =< erlang:length(String2) -> false;
longest_first_case_insensitive( _String1, _String2 ) -> true.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 561 ⟶ 1,685:
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">include sort.e
include wildcard.e
include misc.e
Line 580 ⟶ 1,704:
 
puts(1,"\n\nSorted:\n")
pretty_print(1,custom_sort(routine_id("my_compare"),strings),{2})</langsyntaxhighlight>
 
{{out}}
Output:
<pre>Unsorted:
{
Line 606 ⟶ 1,730:
"to"
}</pre>
 
=={{header|EGL}}==
 
{{works with|EDT|}}
<lang EGL>program SortExample
function main()
test1 string[] = ["Here", "are", "some", "sample", "strings", "to", "be", "sorted"];
test1.sort(sortFunction);
 
SysLib.writeStdout("Test 1:");
for(i int from 1 to test1.getSize())
SysLib.writeStdout(test1[i]);
end
 
test2 string[] = ["Cat", "apple", "Adam", "zero", "Xmas", "quit", "Level", "add", "Actor", "base", "butter"];
test2.sort(sortFunction);
SysLib.writeStdout("Test 2:");
for(i int from 1 to test2.getSize())
SysLib.writeStdout(test2[i]);
end
end
function sortFunction(a any in, b any in) returns (int)
result int = (b as string).length() - (a as string).length();
if (result == 0)
case
when ((a as string).toLowerCase() > (b as string).toLowerCase())
result = 1;
when ((a as string).toLowerCase() < (b as string).toLowerCase())
result = -1;
otherwise
result = 0;
end
end
return result;
end
end</lang>
 
{{out|Output}}
<pre>Test 1:
strings
sample
sorted
Here
some
are
be
to
 
Test 2:
butter
Actor
apple
Level
Adam
base
quit
Xmas
zero
add
Cat</pre>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">let myCompare (s1:string) (s2:string) =
match compare s2.Length s1.Length with
| 0 -> compare (s1.ToLower()) (s2.ToLower())
Line 682 ⟶ 1,741:
let sortedStrings = List.sortWith myCompare strings
 
printfn "%A" sortedStrings</langsyntaxhighlight>
 
{{out}}
Output:
<pre>["strings"; "sample"; "sorted"; "Here"; "some"; "are"; "be"; "to"]</pre>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">: my-compare ( s1 s2 -- <=> )
2dup [ length ] compare invert-comparison
dup +eq+ = [ drop [ >lower ] compare ] [ 2nip ] if ;
 
{ "this" "is" "a" "set" "of" "strings" "to" "sort" } [ my-compare ] sort</langsyntaxhighlight>
 
=={{header|Fantom}}==
Line 698 ⟶ 1,757:
The List's sort method can be customised using a custom comparator. This is a method which returns an Int: -1 for less than, 0 for equal, +1 for greater than.
 
<langsyntaxhighlight lang="fantom">
class Main
{
Line 719 ⟶ 1,778:
}
}
</syntaxhighlight>
</lang>
 
Output:
 
{{out}}
<pre>
$ fan comparator-sort.fan
Line 733 ⟶ 1,791:
Fortran does not have builtin to sort arrays (of numbers or strings), with or without custom comparator; so we need modifying e.g. [[Shell sort#Fortran|this code]] in order to handle strings and to accept a custom comparator.
 
<langsyntaxhighlight lang="fortran">module sorts_with_custom_comparator
implicit none
contains
Line 765 ⟶ 1,823:
end do
end subroutine a_sort
end module sorts_with_custom_comparator</langsyntaxhighlight>
 
Then we have to put our custom comparator in a module (<tt>to_lower</tt> is defined [[Change string case|here]]):
 
<langsyntaxhighlight lang="fortran">module comparators
implicit none
contains
Line 794 ⟶ 1,852:
end if
end function my_compare
end module comparators</langsyntaxhighlight>
 
At the end, we can test these:
 
<langsyntaxhighlight lang="fortran">program CustomComparator
use comparators
use sorts_with_custom_comparator
Line 812 ⟶ 1,870:
print *, trim(str(i))
end do
end program CustomComparator</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' version 23-10-2016
' compile with: fbc -s console
 
#Include Once "crt/stdlib.bi" ' for qsort
 
Function mycmp Cdecl (s1 As Any Pointer, s2 As Any Pointer) As Long
 
' -1 no swap first element before second element
' 0 no swap needed, don't care
' 1 swap first element after second element
 
Dim As String str1 = *Cast(String Ptr, s1)
Dim As String str2 = *Cast(String Ptr, s2)
 
Dim As Long l1 = Len(str1), l2 = Len(str2)
If (l1 > l2) Then Return -1 ' descending
If (l1 < l2) Then Return 1 '
 
' there equal length, sort ascending
If UCase(str1) = UCase(str2) Then
If str1 > str2 Then Return 1
Else
If UCase(str1) > UCase(str2) Then Return 1
End If
Return 0
 
End Function
 
' ------=< MAIN >=------
 
Dim As String words(0 To ...) = {"Here", "are", "some", "sample", _
"strings", "to", "be", "sorted" }
 
Dim As ULong array_size = UBound(words) - LBound(words) + 1
 
qsort(@words(0), array_size, SizeOf(String), @mycmp)
 
For i As Integer = 0 To UBound(words)
Print words(i)
Next
Print
 
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</syntaxhighlight>
{{out}}
<pre>strings
sample
sorted
Here
some
are
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!
<syntaxhighlight 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]]</syntaxhighlight>
{{out}}
<pre>
[strings, sample, sorted, Here, some, are, be, to]
</pre>
 
Alternately, here is a surprisingly powerful version of the sorter above that can sort based on the alphabetization rules of a very wide number of human languages. The language for the lexicographic comparison can be specified to the <CODE>lexicalCompare</CODE> function as an ISO 639-1 two-letter language code, or can be even more specific. For example, the following sorts a list of words based on the alphabetization rules for Danish.
<syntaxhighlight lang="frink">f = {|a,b,lang| lexicalCompare[a,b,lang] }
 
words = ["Ærø", "Aalborg", "Tårnby", "Vejen", "Thisted", "Stevns", "Sønderborg", "Eliasen"]
println[sort[words, f, "da"]]</syntaxhighlight>
{{out}}
<pre>
[Eliasen, Stevns, Sønderborg, Thisted, Tårnby, Vejen, Ærø, Aalborg]
</pre>
 
Note that under the lexicographic ordering rules for Danish, that order is correct, with names beginning with "Aa" alphabetized last. How many other languages handle this correctly?
 
=={{header|FunL}}==
<langsyntaxhighlight lang="funl">def preceeds( a, b ) = b.length() < a.length() or b.length() == a.length() and a.compareToIgnoreCase( b ) < 0
 
println( ["here", "are", "Some", "sample", "strings", "to", "be", "sorted"].sortWith(preceeds) )</langsyntaxhighlight>
 
{{out}}
Line 824 ⟶ 1,970:
["strings", "sample", "sorted", "here", "Some", "are", "be", "to"]
</pre>
 
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
local fn CustomComparator( obj1 as CFTypeRef, obj2 as CFTypeRef, context as ptr ) as NSComparisonResult
NSComparisonResult result = fn StringCaseInsensitiveCompare( obj1, obj2 )
end fn = result
 
local fn ComparatorStringSort( wordString as CFStringRef ) as CFStringRef
CFArrayRef stringArray = fn StringComponentsSeparatedByString( wordString, @" " )
CFArrayRef sortedArray = fn ArraySortedArrayUsingFunction( stringArray, @fn CustomComparator, NULL )
CFStringRef sortedStr = fn ArrayComponentsJoinedByString( sortedArray, @"\n" )
end fn = sortedStr
 
NSLog( @"%@", fn ComparatorStringSort( @"The quick brown fox jumped over the lazy dog's back" ) )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
back
brown
dog's
fox
jumped
lazy
over
quick
The
the
</pre>
 
 
 
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Sort_using_a_custom_comparator}}
 
'''Solution'''
 
[[File:Fōrmulæ - Sort using a custom comparator 01.png]]
 
[[File:Fōrmulæ - Sort using a custom comparator 02.png]]
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 852 ⟶ 2,045:
sort.Sort(s)
fmt.Println(s, "(sorted)")
}</langsyntaxhighlight>
{{out}}
Output:
<pre>[To tell your name the livelong day To an admiring bog] (original)
[admiring livelong name tell your bog day the an To To] (sorted)</pre>
Line 859 ⟶ 2,052:
=={{header|Groovy}}==
The "custom comparator" is just a closure attached to the sort method invocation.
<langsyntaxhighlight lang="groovy">def strings = "Here are some sample strings to be sorted".split()
strings.sort { x, y ->
y.length() <=> x.length() ?: x.compareToIgnoreCase(y)
}
println strings</langsyntaxhighlight>
 
{{out}}
Output:
<pre>[strings, sample, sorted, Here, some, are, be, to]</pre>
 
=={{header|Haskell}}==
{{works with|GHC}}
<langsyntaxhighlight lang="haskell">import ListData.Char (toLower)
import CharData.List (sortBy)
import Data.Ord (comparing)
 
-------------------- CUSTOM COMPARATORS ------------------
mycmp s1 s2 = case compare (length s2) (length s1) of
EQ -> compare (map toLower s1) (map toLower s2)
x -> x
 
lengthThenAZ :: String -> String -> Ordering
strings = ["Here", "are", "some", "sample", "strings", "to", "be", "sorted"]
lengthThenAZ = comparing length <> comparing (fmap toLower)
sorted = sortBy mycmp strings</lang>
 
descLengthThenAZ :: String -> String -> Ordering
Alternate definition of <tt>mycmp</tt> using the <tt>Monoid</tt> instance for <tt>Ordering</tt>:
descLengthThenAZ =
flip (comparing length)
<> comparing (fmap toLower)
 
--------------------------- TEST -------------------------
<lang haskell>import Data.Monoid
main :: IO ()
mycmp s1 s2 = mappend (compare (length s2) (length s1))
main =
(compare (map toLower s1) (map toLower s2))</lang>
mapM_
putStrLn
( fmap
unlines
( [sortBy] <*> [lengthThenAZ, descLengthThenAZ]
<*> [ [ "Here",
"are",
"some",
"sample",
"strings",
"to",
"be",
"sorted"
]
]
)
)</syntaxhighlight>
{{Out}}
<pre>be
to
are
Here
some
sample
sorted
strings
 
strings
sample
sorted
Here
some
are
be
to</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">procedure main() #: demonstrate various ways to sort a list and string
write("Sorting Demo for custom comparator")
L := ["Here", "are", "some", "sample", "strings", "to", "be", "sorted"]
Line 899 ⟶ 2,129:
procedure cmptask(a,b) # sort by descending length and ascending lexicographic order for strings of equal length
if (*a > *b) | ((*a = *b) & (map(a) << map(b))) then return b
end</langsyntaxhighlight>
 
Note(1): This example relies on [[Sorting_algorithms/Bubble_sort#Icon| the supporting procedures 'sortop', and 'demosort' in Bubble Sort]].
<br/>Note(2): This example can utilize any of the sorting algorithms that share the same base code including: [[Sorting_algorithms/Bubble_sort#Icon_and_Unicon|Bubble]], [[Sorting_algorithms/Cocktail_sort#CocktailIcon_and_Unicon|Cocktail]], [[Sorting_algorithms/Comb_sort#Icon_and_Unicon|Comb]], [[Sorting_algorithms/Gnome_sort#Icon_and_Unicon|Gnome]], and [[Sorting_algorithms/Shell_sort#Icon_and_Unicon|Shell]].
<br/>Note(3): Using 'map' in the 'cmptask' procedure would not be efficient on large lists.
 
Note(2): This example can utilize any of the sorting algorithms that share the same base code including: [[Sorting_algorithms/Bubble_sort#Icon_and_Unicon|Bubble]], [[Sorting_algorithms/Cocktail_sort#CocktailIcon_and_Unicon|Cocktail]], [[Sorting_algorithms/Comb_sort#Icon_and_Unicon|Comb]], [[Sorting_algorithms/Gnome_sort#Icon_and_Unicon|Gnome]], and [[Sorting_algorithms/Shell_sort#Icon_and_Unicon|Shell]].
Sample Output:<pre>Sorting Demo for custom comparator
 
Note(3): Using 'map' in the 'cmptask' procedure would not be efficient on large lists.
 
{{out}}
<pre>Sorting Demo for custom comparator
Unsorted Input :
"Here"
Line 926 ⟶ 2,159:
 
=={{header|J}}==
Case-insensitivity is obtained using <tt>lower</tt>, a verb taken from [[Change string case]]. Standard utilities <tt>tolower</tt> or <tt>toupper</tt> may be substituted.
Standard utilities <tt>tolower</tt> or <tt>toupper</tt> may be substituted.
 
<langsyntaxhighlight lang="j"> mycmp=: 1 :'/:u'
length_and_lex =: (-@:# ; lower)&>
strings=: 'Here';'are';'some';'sample';'strings';'to';'be';'sorted'
Line 934 ⟶ 2,168:
+-------+------+------+----+----+---+--+--+
|strings|sample|sorted|Here|some|are|be|to|
+-------+------+------+----+----+---+--+--+</langsyntaxhighlight>
 
Generally speaking, J uses the concept of sorting against a normalized content (which is what <code>length_and_lex</code> provided in the above example). This eliminates a class of errors (which might be conceptualized by using a custom comparator which generates a random number: order would be non-deterministic and sorted order would depend on details of the sorting algorithm) and supports O(n) sorting algorithms such as bin sort (which cannot use comparators).
 
=={{header|Java}}==
{{works with|Java|1.5+}}
<langsyntaxhighlight lang="java5">import java.util.Comparator;
import java.util.Arrays;
 
Line 957 ⟶ 2,193:
System.out.print(s + " ");
}
}</langsyntaxhighlight>
 
Same thing as above
{{works with|Java|8+}}
<langsyntaxhighlight lang="java5">import java.util.Comparator;
import java.util.Arrays;
 
Line 978 ⟶ 2,214:
System.out.print(s + " ");
}
}</langsyntaxhighlight>
 
Using Java 11
<syntaxhighlight lang="java">
import java.util.Comparator;
import java.util.List;
 
public final class SortUsingCustomComparator {
 
public static void main(String[] args) {
List<String> list = List.of( "Here", "are", "some", "sample", "strings", "to", "be", "sorted" );
Comparator<String> custom = Comparator.comparing(String::length, Comparator.reverseOrder())
.thenComparing(Comparator.naturalOrder());
List<String> sortedList = list.stream().sorted(custom).toList();
 
System.out.println(sortedList);
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
[strings, sample, sorted, Here, some, are, be, to]
</pre>
 
=={{header|JavaScript}}==
===ES5===
<lang javascript>function lengthSorter(a, b) {
<syntaxhighlight lang="javascript">function lengthSorter(a, b) {
var result = b.length - a.length;
if (result == 0)
Line 990 ⟶ 2,252:
var test = ["Here", "are", "some", "sample", "strings", "to", "be", "sorted"];
test.sort(lengthSorter);
alert( test.join(' ') ); // strings sample sorted Here some are be to</langsyntaxhighlight>
 
Or, abstracting a little for simpler composition of compound and derived searches (ASC and DESC, secondary sorts):
 
<syntaxhighlight lang="javascript">(function () {
'use strict';
 
// GENERIC FUNCTIONS FOR COMPARISONS
 
// Ordering :: ( LT | EQ | GT ) | ( -1 | 0 | 1 )
 
// compare :: a -> a -> Ordering
var compare = function (a, b) {
return a < b ? -1 : a > b ? 1 : 0;
};
 
// mappendOrdering :: Ordering -> Ordering -> Ordering
var mappendOrdering = function (a, b) {
return a !== 0 ? a : b;
};
 
// on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
var on = function (f, g) {
return function (a, b) {
return f(g(a), g(b));
};
};
 
// flip :: (a -> b -> c) -> b -> a -> c
var flip = function (f) {
return function (a, b) {
return f.apply(null, [b, a]);
};
};
 
// arrayCopy :: [a] -> [a]
var arrayCopy = function (xs) {
return xs.slice(0);
};
 
// show :: a -> String
var show = function (x) {
return JSON.stringify(x, null, 2);
};
 
// TEST
var xs = ['Shanghai', 'Karachi', 'Beijing', 'Sao Paulo', 'Dhaka', 'Delhi', 'Lagos'];
 
var rs = [{
name: 'Shanghai',
pop: 24.2
}, {
name: 'Karachi',
pop: 23.5
}, {
name: 'Beijing',
pop: 21.5
}, {
name: 'Sao Paulo',
pop: 24.2
}, {
name: 'Dhaka',
pop: 17.0
}, {
name: 'Delhi',
pop: 16.8
}, {
name: 'Lagos',
pop: 16.1
}];
 
// population :: Dictionary -> Num
var population = function (x) {
return x.pop;
};
 
// length :: [a] -> Int
var length = function (xs) {
return xs.length;
};
 
// toLower :: String -> String
var toLower = function (s) {
return s.toLowerCase();
};
 
// lengthThenAZ :: String -> String -> ( -1 | 0 | 1)
var lengthThenAZ = function (a, b) {
return mappendOrdering(
on(compare, length)(a, b),
on(compare, toLower)(a, b)
);
};
 
// descLengthThenAZ :: String -> String -> ( -1 | 0 | 1)
var descLengthThenAZ = function (a, b) {
return mappendOrdering(
on(flip(compare), length)(a, b),
on(compare, toLower)(a, b)
);
};
 
return show({
default: arrayCopy(xs)
.sort(compare),
 
descendingDefault: arrayCopy(xs)
.sort(flip(compare)),
 
byLengthThenAZ: arrayCopy(xs)
.sort(lengthThenAZ),
 
byDescendingLengthThenZA: arrayCopy(xs)
.sort(flip(lengthThenAZ)),
 
byDescendingLengthThenAZ: arrayCopy(xs)
.sort(descLengthThenAZ),
 
byPopulation: arrayCopy(rs)
.sort(on(compare, population)),
 
byDescendingPopulation: arrayCopy(rs)
.sort(on(flip(compare), population))
});
})();</syntaxhighlight>
 
===ES6===
<syntaxhighlight lang="javascript">(() => {
'use strict';
 
// main :: IO ()
const main = () => {
const
lengthThenAZ = mappendOrd(
comparing(length),
comparing(toLower)
),
descLengthThenAZ = mappendOrd(
flip(comparing(length)),
comparing(toLower)
);
 
console.log(
apList(apList([sortBy])([
lengthThenAZ,
descLengthThenAZ
]))([
[
"Here", "are", "some", "sample",
"strings", "to", "be", "sorted"
]
]).map(unlines).join('\n\n')
);
};
 
// GENERIC FUNCTIONS ----------------------------------
 
// apList (<*>) :: [a -> b] -> [a] -> [b]
const apList = fs => xs =>
// The application of each of a list of functions,
// to each of a list of values.
fs.flatMap(
f => xs.flatMap(x => [f(x)])
);
 
// comparing :: (a -> b) -> (a -> a -> Ordering)
const comparing = f =>
(x, y) => {
const
a = f(x),
b = f(y);
return a < b ? -1 : (a > b ? 1 : 0);
};
 
// flip :: (a -> b -> c) -> b -> a -> c
const flip = f =>
1 < f.length ? (
(a, b) => f(b, a)
) : (x => y => f(y)(x));
 
// length :: [a] -> Int
const length = xs =>
(Array.isArray(xs) || 'string' === typeof xs) ? (
xs.length
) : Infinity;
 
// mappendOrd (<>) :: Ordering -> Ordering -> Ordering
const mappendOrd = (a, b) => a !== 0 ? a : b;
 
// sortBy :: (a -> a -> Ordering) -> [a] -> [a]
const sortBy = f => xs =>
xs.slice()
.sort(f);
 
// toLower :: String -> String
const toLower = s => s.toLocaleLowerCase();
 
// unlines :: [String] -> String
const unlines = xs => xs.join('\n');
 
// MAIN ---
return main();
})();</syntaxhighlight>
{{Out}}
<pre>be
to
are
Here
some
sample
sorted
strings
 
strings
sample
sorted
Here
some
are
be
to</pre>
 
=={{header|jq}}==
The comparator, cmp, must have 0 arity, and may either be boolean or follow the negative/zero/positive convention.
 
If "o" is an ordering, and if x and y are two entities for which "x o y" is defined, then "[x,y] | cmp" should return a number, or a boolean value.
 
As illustrated in the example, the comparator may be any jq filter, whether or not it is defined as a function.
<syntaxhighlight lang="jq">def quicksort(cmp):
if length < 2 then . # it is already sorted
else .[0] as $pivot
| reduce .[] as $x
# state: [less, equal, greater]
( [ [], [], [] ]; # three empty arrays:
if $x == $pivot then .[1] += [$x] # add x to equal
else ([$x,$pivot]|cmp) as $order
| if $order == 0 then .[1] += [$x] # ditto
elif ($order|type) == "number" then
if $order < 0 then .[0] += [$x] # add x to less
else .[2] += [$x] # add x to greater
end
else ([$pivot,$x]|cmp) as $order2
| if $order and $order2 then .[1] += [$x] # add x to equal
elif $order then .[0] += [$x] # add x to less
else .[2] += [$x] # add x to greater
end
end
end )
| (.[0] | quicksort(cmp) ) + .[1] + (.[2] | quicksort(cmp) )
end ;</syntaxhighlight>
Example:
<syntaxhighlight lang="jq"># Sort by string length, breaking ties using ordinary string comparison.
["z", "yz", "ab", "c"]
| quicksort( (.[0]|length) > (.[1]|length) or ( (.[0]|length) == (.[1]|length) and .[0] < .[1] ) )
</syntaxhighlight>
{{Out}}
<syntaxhighlight lang="jq">[
"ab",
"yz",
"c",
"z"
]</syntaxhighlight>
 
=={{header|Julia}}==
My word list source is the opening sentence of Shelly's [http://www.gutenberg.org/cache/epub/84/pg84.txt Frankenstein].
<syntaxhighlight lang="julia">wl = filter(!isempty, split("""You will rejoice to hear that no disaster has accompanied the
commencement of an enterprise which you have regarded with such evil
forebodings.""", r"\W+"))
 
println("Original list:\n - ", join(wl, "\n - "))
sort!(wl; by=x -> (-length(x), lowercase(x)))
println("\nSorted list:\n - ", join(wl, "\n - "))
</syntaxhighlight>
 
{{out}}
<pre>Original List:
You
will
rejoice
to
hear
that
no
disaster
has
accompanied
the
commencement
of
an
enterprise
which
you
have
regarded
with
such
evil
forebodings
 
Sorted List:
commencement
accompanied
forebodings
enterprise
disaster
regarded
rejoice
which
evil
have
hear
such
that
will
with
has
the
You
you
an
no
of
to
</pre>
 
=={{header|Kotlin}}==
A translation from Java, also showing the seamless interop between Java and Kotlin code.
 
<syntaxhighlight lang="kotlin">import java.util.Arrays
 
fun main(args: Array<String>) {
val strings = arrayOf("Here", "are", "some", "sample", "strings", "to", "be", "sorted")
 
fun printArray(message: String, array: Array<String>) = with(array) {
print("$message [")
forEachIndexed { index, string ->
print(if (index == lastIndex) string else "$string, ")
}
println("]")
}
 
printArray("Unsorted:", strings)
 
Arrays.sort(strings) { first, second ->
val lengthDifference = second.length - first.length
if (lengthDifference == 0) first.lowercase().compareTo(second.lowercase(), true) else lengthDifference
}
 
printArray("Sorted:", strings)
}</syntaxhighlight>
 
{{out}}
<pre>Unsorted: [Here, are, some, sample, strings, to, be, sorted]
Sorted: [strings, sample, sorted, Here, some, are, be, to]</pre>
 
 
A more idiomatic version (1.3):
 
<syntaxhighlight lang="kotlin">fun main(args: Array<String>) {
val strings = listOf("Here", "are", "some", "sample", "strings", "to", "be", "sorted")
println("Unsorted: $strings")
 
// sort by content first then by length => no need for a custom comparator since sortedByDescending is stable
val sorted = strings.sortedBy { it.lowercase() }.sortedByDescending { it.length }
 
println("Sorted: $sorted")
}</syntaxhighlight>
 
 
Using a custom comparator as requested by task description:
 
<syntaxhighlight lang="kotlin">fun main(args: Array<String>) {
val strings = listOf("Here", "are", "some", "sample", "strings", "to", "be", "sorted")
println("Unsorted: $strings")
val sorted = strings.sortedWith { a, b ->
compareValues(b.length, a.length).let {
if (it == 0) compareValues(a.lowercase(), b.lowercase())
else it
}
}
println("Sorted: $sorted")
}</syntaxhighlight>
 
 
Faster when computing length and lowercase only once per value ([[wp:Schwartzian transform|Schwartzian transform]]):
 
<syntaxhighlight lang="kotlin">fun main(args: Array<String>) {
val strings = listOf("Here", "are", "some", "sample", "strings", "to", "be", "sorted")
println("Unsorted: $strings")
 
val sorted = strings.map { Triple(it, it.length, it.lowercase()) }.sortedWith { a, b ->
compareValues(b.second, a.second).let {
if (it == 0) compareValues(a.third, b.third)
else it
}
}.map { it.first }
 
println("Sorted: $sorted")
}</syntaxhighlight>
 
 
{{out}}
<pre>Unsorted: [Here, are, some, sample, strings, to, be, sorted]
Sorted: [strings, sample, sorted, Here, some, are, be, to]</pre>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
 
{def sortbylength
 
{def sortbylength.i
{lambda {:x :a}
{if {A.empty? :a}
then {A.new :x}
else {if {> {W.length :x} {W.length {A.first :a}}}
then {A.addfirst! :x :a}
else {A.addfirst! {A.first :a}
{sortbylength.i :x {A.rest :a}}} }}}}
 
{def sortbylength.r
{lambda {:a1 :a2}
{if {A.empty? :a1}
then :a2
else {sortbylength.r {A.rest :a1}
{sortbylength.i {A.first :a1} :a2}} }}}
 
{lambda {:s}
{S.replace (\[|\]) by in
{S.replace , by space in
{A.disp {sortbylength.r {A.new :s} {A.new}} }}}}}
-> sortbylength
 
{sortbylength here are Some sample strings to be sorted}
-> strings sample sorted here Some are to be
</syntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">test = { "Here", "we", "have", "some", "sample", "strings", "to", "be", "sorted" }
 
function stringSorter(a, b)
Line 1,004 ⟶ 2,703:
 
-- print sorted table
for k,v in pairs(test) do print(v) end</langsyntaxhighlight>
 
{{out}}
Output:
<pre>strings sample sorted have Here some be to we</pre>
 
=={{header|M2000 Interpreter}}==
Report statement print document but stop at 3/4 of console lines waiting keypress or space to show more lines. So when run this example press space to continue. Clipboard has the output too.
 
 
<syntaxhighlight lang="m2000 interpreter">
Module Checkit {
Class Quick {
Private:
partition=lambda-> {
Read &A(), p, r : i = p-1 : x=A(r)
For j=p to r-1 {If .LE(A(j), x) Then i++:Swap A(i),A(j)
} : Swap A(i+1), A(r) : Push i+2, i
}
Public:
LE=Lambda->Number<=Number
Module ForStrings {
.partition<=lambda-> {
Read &a$(), p, r : i = p-1 : x$=a$(r)
For j=p to r-1 {If a$(j)<= x$ Then i++:Swap a$(i),a$(j)
} : Swap a$(i+1), a$(r) : Push i+2, i
}
}
Function quicksort {
Read ref$
{
loop : If Stackitem() >= Stackitem(2) Then Drop 2 : if empty then {Break} else continue
over 2,2 : call .partition(ref$) :shift 3
}
}
}
Quick=Quick()
ToSort$="this is a set of strings to sort This Is A Set Of Strings To Sort"
Dim a$()
a$()=Piece$(ToSort$, " ")
\\ we can redim to any range
Dim a$(100 to len(a$())+99) ' from 100 to 115 (16 items)
Group Quick {
Module ForStringsSpecial {
.partition<=lambda-> {
Read &a$(), p, r : i = p-1 : x$=a$(r) :lx$=lcase$(x$) : k=len(x$)
For j=p to r-1 {
m=len(a$(j))
select case compare(m, k)
case 0
{
aj$=lcase$(a$(j))
if aj$>lx$ then exit
if aj$=lx$ then if a$(j)<=x$ then exit
i++
Swap a$(i),a$(j)
}
case 1
{
i++:Swap a$(i),a$(j)
}
End Select
} : Swap a$(i+1), a$(r) : Push i+2, i
}
}
}
Document doc$={Unsorted List:
}
k=each(a$())
While k {
doc$=" "+array$(k)+{
}
}
Quick.ForStringsSpecial
\\ Dimension(a$(), 0, 1) is Lbound a$() first dimension
\\ Dimension(a$(), 0, 1) is Ubound a$() first dimension
Call Quick.quicksort(&a$(), Dimension(a$(), 0, 1), Dimension(a$(), 1,1))
k=each(a$())
Doc$={
Sorted List:
}
While k {
doc$=" "+array$(k)+{
}
}
Report doc$
Clipboard doc$
}
Checkit
</syntaxhighlight>
 
ForStringsSpecial can be coded using a Compare(aj$, lx$). See the use of break to break cases in select cases.
Any case in Select case may have one statement (if then is one statement), or a block of code. We can leave a case with a blank line after, a one statement line, or a block of code, or a case statement. A break statement break cases, so all code executed, until a continue found, to exit from Select (next statement after End Select). We use a sub to make two statements as one.
 
<syntaxhighlight lang="m2000 interpreter">
Group Quick {
Module ForStringsSpecial {
.partition<=lambda-> {
Read &a$(), p, r : i = p-1 : x$=a$(r) :lx$=lcase$(x$) : k=len(x$)
For j=p to r-1 {
m=len(a$(j))
select case compare(m, k)
case 0
{
aj$=lcase$(a$(j))
\\ in Case the Break statement execute all cases until a case has a Continue
select case compare(aj$, lx$)
case 0
if a$(j)>x$ then break
Case 1
swapit()
End Select
}
case 1
swapit()
End Select
} : Swap a$(i+1), a$(r) : Push i+2, i
Sub swapit()
i++:Swap a$(i),a$(j)
End Sub
}
}
}
</syntaxhighlight>
 
{{out}}
<pre>
Unsorted List:
this
is
a
set
of
strings
to
sort
This
Is
A
Set
Of
Strings
To
Sort
 
Sorted List:
strings
Strings
sort
Sort
this
This
set
Set
is
Is
of
Of
to
To
a
A
</pre>
 
=={{header|Maple}}==
<syntaxhighlight lang="maple">Compare_fn:= proc(s1, s2)
local len1, len2;
len1 := StringTools:-Length(s1);
len2 := StringTools:-Length(s2);
if (len1 > len2) then
return true;
elif (len1 < len2) then
return false;
else # ascending lexicographic order for strings of equal length / case insensitive
StringTools:-CompareCI(s1, s2);
end if;
end proc:
 
L := ["Here", "are", "some", "sample", "strings", "to", "be", "sorted", "Tooo"];
sort(L, Compare_fn);</syntaxhighlight>
{{out}}
<pre>
["strings", "sample", "sorted", "Here", "some", "Tooo", "are", "be", "to"]
</pre>
 
<lang lua>strings sample sorted have Here some be to we</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
We define a new function to give true or false if two elements are in order. After that we can simply use the built-in Sort with an ordering function:
After that we can simply use the built-in Sort with an ordering function:
<lang Mathematica>StringOrderQ[x_String, y_String] :=
<syntaxhighlight lang="mathematica">StringOrderQ[x_String, y_String] :=
If[StringLength[x] == StringLength[y],
OrderedQ[{x, y}],
Line 1,018 ⟶ 2,898:
]
words={"on","sunday","sander","sifted","and","sorted","sambaa","for","a","second"};
Sort[words,StringOrderQ]</langsyntaxhighlight>
gives back:
<pre>{sambaa,sander,second,sifted,sorted,sunday,and,for,on,a}</pre>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">strangeorderp(a, b) := slength(a) > slength(b) or (slength(a) = slength(b) and orderlessp(a, b))$
s: tokens("Lorem ipsum dolor sit amet consectetur adipiscing elit Sed non risus Suspendisse\
lectus tortor dignissim sit amet adipiscing nec ultricies sed dolor")$
Line 1,030 ⟶ 2,910:
["Suspendisse", "consectetur", "adipiscing", "adipiscing", "dignissim", "ultricies",
"lectus", "tortor", "Lorem", "dolor", "dolor", "ipsum", "risus", "amet", "amet",
"elit", "Sed", "nec", "non", "sed", "sit", "sit"]</langsyntaxhighlight>
 
=={{header|MAXScript}}==
<langsyntaxhighlight lang="maxscript">fn myCmp str1 str2 =
(
case of
Line 1,055 ⟶ 2,935:
strList = #("Here", "are", "some", "sample", "strings", "to", "be", "sorted")
qSort strList myCmp
print strList</langsyntaxhighlight>
 
=={{header|min}}==
{{works with|min|0.19.3}}
<syntaxhighlight lang="min">("Here" "are" "some" "sample" "strings" "to" "be" "sorted")
(((length) (length)) spread <) sort print</syntaxhighlight>
{{out}}
<pre>
("strings" "sample" "sorted" "Here" "some" "are" "to" "be")
</pre>
 
=={{header|Nemerle}}==
<langsyntaxhighlight Nemerlelang="nemerle">using System.Console;
 
module CustomSort
Line 1,070 ⟶ 2,959:
WriteLine(strings2.Sort((x, y) => x.CompareTo(y)))
}
}</langsyntaxhighlight>
{{out}}
Output:
<pre>[different, strings, length, these, are, of]
[apple, chewy, House, Later, rises, Salty]</pre>
Line 1,077 ⟶ 2,966:
=={{header|NetRexx}}==
{{trans|Java}}
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 1,109 ⟶ 2,998:
else signal IllegalArgumentException('Arguments must be Strings')
return cRes
</syntaxhighlight>
</lang>
{{out}}
'''Output:'''
<pre>
[Here,are,some,sample,strings,to,be,sorted]
Line 1,117 ⟶ 3,006:
 
=={{header|Nial}}==
<langsyntaxhighlight lang="nial">sort fork [=[tally first,tally last],up, >= [tally first,tally last]] ['Here', 'are', 'some', 'sample', 'strings', 'to', 'be', 'sorted']
=+-------+------+------+----+----+---+--+--+
=|strings|sample|sorted|Here|some|are|be|to|
=+-------+------+------+----+----+---+--+--+</langsyntaxhighlight>
 
=={{header|NimrodNim}}==
<langsyntaxhighlight nimrodlang="nim">import strutils, algorithm
 
var strings = "here are Some sample strings to be sorted".split(' ')
 
strings.sort(proc (x, y: string): int =
result = cmp(y.len, x.len))
if result == 0:
result = cmpIgnoreCase(x, y)
)
 
echo strings</langsyntaxhighlight>
 
{{out}}
<pre>@["strings", "sample", "sorted", "here", "Some", "are", "be", "to"]</pre>
 
=={{header|Objeck}}==
<syntaxhighlight lang="objeck">use Collection;
 
class Test {
function : Main(args : String[]) ~ Nil {
v := CreateHolders(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"]);
"unsorted: "->Print(); Show(v);
v->Sort();
"sorted: "->Print(); Show(v);
}
function : CreateHolders(strings : String[]) ~ CompareVector {
vector := CompareVector->New();
each(i : strings) {
vector->AddBack(StringHolder->New(strings[i]));
};
return vector;
}
function : Show(v : CompareVector) ~ Nil {
each(i : v) {
s := v->Get(i)->As(StringHolder);
s->ToString()->Print();
if(i + 1 < v->Size()) {
','->Print();
};
};
'\n'->Print();
}
}
 
class StringHolder implements Compare {
@s : String;
New(s : String) {
@s := s;
}
method : public : Compare(c : Compare) ~ Int {
h := c->As(StringHolder);
r := h->ToString();
size := r->Size() - @s->Size();
if(size = 0) {
size := @s->ToUpper()->Compare(r->ToUpper());
};
return size;
}
method : public : HashID() ~ Int {
return @s->HashID();
}
method : public : ToString() ~ String {
return @s;
}
}</syntaxhighlight>
 
<pre>
unsorted: Here,are,some,sample,strings,to,be,sorted
sorted: strings,sample,sorted,Here,some,are,be,to
</pre>
 
=={{header|Objective-C}}==
{{works with|Cocoa|Mac OS X 10.6+}}
Using blocks:
<langsyntaxhighlight lang="objc">#import <Foundation/Foundation.h>
 
#define esign(X) (((X)>0)?1:(((X)<0)?-1:0))
Line 1,162 ⟶ 3,121:
}
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
 
Line 1,168 ⟶ 3,127:
 
{{works with|Cocoa}}
<langsyntaxhighlight lang="objc">#import <Foundation/Foundation.h>
 
@interface NSString (CustomComp)
Line 1,203 ⟶ 3,162:
}
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
This example can also be written using sort descriptors:
{{works with|GNUstep}}
{{works with|Cocoa}}
<langsyntaxhighlight lang="objc">#import <Foundation/Foundation.h>
 
int main()
Line 1,225 ⟶ 3,184:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">let mycmp s1 s2 =
if String.length s1 <> String.length s2 then
compare (String.length s2) (String.length s1)
else
String.compare (String.lowercase s1) (String.lowercase s2)</langsyntaxhighlight>
 
List:
<langsyntaxhighlight lang="ocaml"># let strings = ["Here"; "are"; "some"; "sample"; "strings"; "to"; "be"; "sorted"];;
val strings : string list =
["Here"; "are"; "some"; "sample"; "strings"; "to"; "be"; "sorted"]
# List.sort mycmp strings;;
- : string list =
["strings"; "sample"; "sorted"; "Here"; "some"; "are"; "be"; "to"]</langsyntaxhighlight>
 
Array:
<langsyntaxhighlight lang="ocaml"># let strings = [|"Here"; "are"; "some"; "sample"; "strings"; "to"; "be"; "sorted"|];;
val strings : string array =
[|"Here"; "are"; "some"; "sample"; "strings"; "to"; "be"; "sorted"|]
Line 1,250 ⟶ 3,209:
# strings;;
- : string array =
[|"strings"; "sample"; "sorted"; "Here"; "some"; "are"; "be"; "to"|]</langsyntaxhighlight>
 
=={{header|ooRexxOforth}}==
<lang ooRexx>/* REXX ***************************************************************
* Using the powerful methods of ooRexx
* Sample data taken from REXX
* 28.07.2013 Walter Pachl
**********************************************************************/
myArray = .array~of('---The seven deadly sins---',,
'===========================',,
'pride','avarice','wrath','envy','gluttony','sloth','lust');
Call show 'before sort'
myArray~sort
Call show 'after sort'
myArray~sortWith(.mycmp~new)
Call show 'after custom sort'
Exit
 
<syntaxhighlight lang="oforth">String method: customCmp(s)
show:
s size self size > ifTrue: [ true return ]
Parse Arg txt
s size self size < ifTrue: [ false return ]
Say txt
s toUpper self toUpper <= ;
do name over myArray
say name
end
Return
 
["this", "is", "a", "set", "of", "strings", "to", "sort", "This", "Is", "A", "Set", "Of", "Strings", "To", "Sort"]
::CLASS mycmp MIXINCLASS Comparator
sortWith(#customCmp) println</syntaxhighlight>
::METHOD compare
 
/**********************************************************************
{{out}}
* shorter string considered higher
* when lengths are equal: caseless 'Z' considered higher than 'X' etc.
* Result: 1 B consider higher than A
* -1 A consider higher than B
* 0 A==B (caseless)
**********************************************************************/
Parse Upper Arg A,B
A=strip(A)
B=strip(B)
I = length(A)
J = length(B)
Select
When I << J THEN res=1
When I >> J THEN res=-1
When A >> B THEN res=1
When A << B THEN res=-1
Otherwise res=0
End
RETURN res</lang>
Output:
<pre>
[Strings, strings, Sort, sort, this, This, Set, set, is, Is, of, Of, To, to, A, a]
before sort
</pre>
---The seven deadly sins---
 
===========================
=={{header|Ol}}==
pride
<syntaxhighlight lang="scheme">
avarice
(import (scheme char))
wrath
 
envy
(define (comp a b)
(let ((la (string-length a))
(lb (string-length b)))
(or
(> la lb)
(and (= la lb) (string-ci<? a b)))))
 
(print
(sort comp '(
"lorem" "ipsum" "dolor" "sit" "amet" "consectetur"
"adipiscing" "elit" "maecenas" "varius" "sapien"
"vel" "purus" "hendrerit" "vehicula" "integer"
"hendrerit" "viverra" "turpis" "ac" "sagittis"
"arcu" "pharetra" "id")))
</syntaxhighlight>
 
=={{header|ooRexx}}==
<syntaxhighlight lang="oorexx">A=.array~of('The seven deadly sins','Pride','avarice','Wrath','envy','gluttony','sloth','Lust')
say 'Sorted in order of descending length, and in ascending lexicographic order'
say A~sortWith(.DescLengthAscLexical~new)~makeString
::class DescLengthAscLexical mixinclass Comparator
::method compare
use strict arg left, right
if left~length==right~length
then return left~caselessCompareTo(right)
else return right~length-left~length</syntaxhighlight>
{{out}}
<pre>
Sorted in order of descending length, and in ascending lexicographic order
The seven deadly sins
gluttony
sloth
lust
after sort
---The seven deadly sins---
===========================
avarice
Pride
envy
gluttony
lust
pride
sloth
wrath
after custom sort
---The seven deadly sins---
===========================
gluttony
avarice
pride
sloth
Wrath
wrath
envy
Lust
lust
</pre>
 
=={{header|OxygenBasic}}==
<syntaxhighlight lang="text">
uses generics 'containing sort macros
uses console
string sdata={"CC","Aa","aAa","bb","bbB","b","B","c","A"}
'
int count = countof sdata
'
macro filter(f,a)
=================
'sdata[a]
f=1 'allow all
end macro
'
macro compare(f,a,b)
====================
int la=len sdata[a]
int lb=len sdata[b]
if la<lb
f=1 'descending length
elseif la>lb
'
elseif ucase(sdata[a])>ucase(sdata[b])
f=1 'ascending but case insensitive
endif
end macro
'
NewSortIndex(index,count,rcount,filter,compare)
NewSortedData(sorted,sdata,index,rcount)
'
print "Count: " rcount cr cr
int i
for i=1 to rcount
print sorted[i] cr
next
pause
</syntaxhighlight>
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">declare
fun {LexicographicLessThan Xs Ys}
for
Line 1,353 ⟶ 3,329:
Strings = ["Here" "are" "some" "sample" "strings" "to" "be" "sorted"]
in
{ForAll {Sort Strings LessThan} System.showInfo}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">cmp(a,b)=if(#a<#b,1,if(#a>#b,-1,lex(a,b)));
vecsort(v,cmp)</langsyntaxhighlight>
 
=={{header|Pascal}}==
{{works with|Free Pascal}}
<syntaxhighlight lang="pascal">
program CustomComparator;
{$mode objfpc}{$h+}
uses
Classes, SysUtils, Math;
 
function Compare(List: TStringList; Index1, Index2: Integer): Integer;
begin
Result := CompareValue(Length(List[Index2]), Length(List[Index1]));
if Result = 0 then
Result := CompareText(List[Index1], List[Index2]);
end;
 
const
Sample = 'Here are some sample strings to be sorted';
 
begin
with TStringList.Create do
try
AddStrings(Sample.Split([' '], TStringSplitOptions.ExcludeEmpty));
CustomSort(@Compare);
WriteLn(string.Join(', ', ToStringArray));
finally
Free;
end;
Readln;
end.
</syntaxhighlight>
{{out}}
<pre>
strings, sample, sorted, Here, some, are, be, to
</pre>
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">use feature 'say';
{{works with|Perl|5.8.6}}
<lang perl>sub mycmp { length $b <=> length $a || lc $a cmp lc $b }
 
my @strings = ("qw/Here", "are", "some", "sample", "strings", "to", "be", "sorted")/;
my @sorted = sort mycmp @strings;</lang>
 
# with a subroutine:
Or inline:
sub mycmp { length $b <=> length $a || lc $a cmp lc $b }
<lang perl>my @strings = qw/here are some sample strings to be sorted/;
mysay @sortedjoin =' ', sort {length $b <=> length $a || lc $a cmp lc $b}mycmp @strings</lang>;
 
# inline:
Faster with a [[wp:Schwartzian transform|Schwartzian transform]]:
say join ' ', sort {length $b <=> length $a || lc $a cmp lc $b} @strings
<lang perl>my @strings = qw/here are some sample strings to be sorted/;
 
my @sorted = map { $_->[0] }
# for large inputs, can be faster with a 'Schwartzian' transform:
sort { $a->[1] <=> $b->[1] || $a->[2] cmp $b->[2] }
say join ' ', map { $_->[0] }
sort { $b->[1] <=> $a->[1] || $a->[2] cmp $b->[2] }
map { [ $_, length, lc ] }
@strings;</langsyntaxhighlight>
{{out}}
<pre>strings sample sorted Here some are be to
strings sample sorted Here some are be to
strings sample sorted Here some are be to</pre>
 
=={{header|Perl 6Phix}}==
{{libheader|Phix/basics}}
<lang perl6>my @strings = <Here are some sample strings to be sorted>;
<!--<syntaxhighlight lang="phix">-->
my @sorted_strings = sort { $^a.chars <=> $^b.chars or $^a.lc cmp $^b.lc }, @strings;
<span style="color: #008080;">function</span> <span style="color: #000000;">my_compare</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
.say for @sorted_strings;</lang>
<span style="color: #004080;">integer</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #7060A8;">compare</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- descending length</span>
This behavior is triggered by use of an arity 2 sort routine.
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<p>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">compare</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">lower</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">lower</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- ascending lexical within same length</span>
If instead the function you feed to <code>sort</code> is of arity 1, it will do the Schwartzian transform for you, automatically sorting numeric fields numerically, and strings fields stringily:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<lang perl6>my @sorted_strings = sort -> $x { [ $x.chars, $x.lc ] }, @strings;</lang>
<span style="color: #008080;">return</span> <span style="color: #000000;">c</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">custom_sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">my_compare</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"Here"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"are"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"some"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"sample"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"strings"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"to"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"be"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"sorted"</span><span style="color: #0000FF;">})</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
{"strings","sample","sorted","Here","some","are","be","to"}
</pre>
 
=={{header|PHP}}==
{{works with|PHP|4.4.4 CLI}}
<langsyntaxhighlight lang="php"><?php
function mycmp($s1, $s2)
{
Line 1,398 ⟶ 3,421:
$strings = array("Here", "are", "some", "sample", "strings", "to", "be", "sorted");
usort($strings, "mycmp");
?></langsyntaxhighlight>
 
=={{header|PicoLisp}}==
Line 1,404 ⟶ 3,427:
PicoLisp returns an ascending list (of any type). To get a result in descending
order, the "greater than" function can be supplied
<langsyntaxhighlight PicoLisplang="picolisp">: (sort '("def" "abc" "ghi") >)
-> ("ghi" "def" "abc")</langsyntaxhighlight>
or simply the result reversed (which is, btw, the most efficient way)
<langsyntaxhighlight PicoLisplang="picolisp">: (flip (sort '("def" "abc" "ghi")))
-> ("ghi" "def" "abc")</langsyntaxhighlight>
 
=={{header|PL/I}}==
Line 1,414 ⟶ 3,437:
 
'''Platform:''' [[WIN]]
<langsyntaxhighlight lang="pli">MRGEPKG: package exports(MERGESORT,MERGE,RMERGE);
 
DCL (T(4)) CHAR(20) VAR; /* scratch space of length N/2 */
Line 1,483 ⟶ 3,506:
 
put skip;
END RMERGE;</langsyntaxhighlight>
 
=={{header|Pop11}}==
<langsyntaxhighlight lang="pop11">lvars ls = ['Here' 'are' 'some' 'sample' 'strings' 'to' 'be' 'sorted'];
define compare(s1, s2);
lvars k = length(s2) - length(s1);
Line 1,506 ⟶ 3,529:
l2 = length(s2);
l1 > l2 or (l1 == l2 and alphabefore(uppertolower(s1), uppertolower(s2)))
enddefine;</langsyntaxhighlight>
 
=={{header|PowerBASIC}}==
Line 1,512 ⟶ 3,535:
{{works with|PB/CC|4}}
 
<langsyntaxhighlight lang="powerbasic">FUNCTION Sorter(p1 AS STRING, p2 AS STRING) AS LONG
'if p1 should be first, returns -1
'if p2 should be first, returns 1
Line 1,537 ⟶ 3,560:
'pb's built-in sorting; "USING" tells it to use our custom comparator
ARRAY SORT x(), USING Sorter()
END FUNCTION</langsyntaxhighlight>
 
=={{header|PowerShell}}==
The <code>Sort-Object</code> cmdlet accepts script blocks as arguments as well as multiple criteria after which to sort.
<langsyntaxhighlight lang="powershell">$list = "Here", "are", "some", "sample", "strings", "to", "be", "sorted"
$list | Sort-Object {-$_.Length},{$_}</langsyntaxhighlight>
The negated string length is the first sort criterion, the second is the string itself, resulting in descending length and ascending lexicographic order.
 
=={{header|Prolog}}==
Works with SWI-Prolog (Tested on Version 8.1.19). Duplicates (if any) are removed.
<langsyntaxhighlight Prologlang="prolog">rosetta_sort :-
L = ["Here", "are", "some", "sample", "strings", "to", "be", "sorted" ],
predsort(my_comp, L, L1),
Line 1,557 ⟶ 3,580:
 
my_comp(Comp, W1, W2) :-
lengthstring_length(W1,L1),
lengthstring_length(W2, L2),
( L1 < L2 -> Comp = '>'
; L1 > L2 -> Comp = '<'
Line 1,565 ⟶ 3,588:
my_write(W) :-
format('~s ', [W]).
</syntaxhighlight>
</lang>
 
{{out}}
Output :
<pre> ?- rosetta_sort.
Input list :
Line 1,576 ⟶ 3,599:
true.
</pre>
 
=={{header|Python}}==
Using a key function is usually more efficient than a comparator. We can take advantage of the fact that tuples are ordered first by the first element, then by the second, etc., to perform a sort on multiple criteria.
<langsyntaxhighlight lang="python">strings = "here are Some sample strings to be sorted".split()
 
def mykey(x):
return -len(x), x.upper()
 
print sorted(strings, key=mykey)</langsyntaxhighlight>
 
{{out}}
'''Sample output:'''
<langsyntaxhighlight lang="python">['strings', 'sample', 'sorted', 'here', 'Some', 'are', 'be', 'to']</langsyntaxhighlight>
 
===Alternative method using cmp===
To technically comply with this task, we can also use an actual comparator (''cmp'') function which will be called every time members of the original list are to be compared. Note that this feature is worse than using the key argument and has been removed from Python 3, so should no longer be used in new code.
<langsyntaxhighlight lang="python">def mycmp(s1, s2):
return cmp(len(s2), len(s1)) or cmp(s1.upper(), s2.upper())
 
print sorted(strings, cmp=mycmp)</langsyntaxhighlight>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> [ $ "" swap
witheach
[ upper join ] ] is upper$ ( $ --> )
 
[ over size over size
2dup = iff
[ 2drop upper$
swap upper$ $< ]
else
[ 2swap 2drop < ] ] is comparator ( $ $ -- b )
 
$ ‘here are Some sample strings to be sorted’
nest$ sortwith comparator
witheach [ echo$ sp ]
cr cr
$ "sharna pax and hed on a poal when the ardship of Cambry come out of his hoal"
nest$ sortwith comparator
witheach [ echo$ sp ]</syntaxhighlight>
 
{{out}}
 
<pre>strings sample sorted here Some are be to
 
ardship Cambry sharna come hoal poal when and hed his out pax the of of on a </pre>
 
=={{header|R}}==
 
<langsyntaxhighlight Rlang="r">v = c("Here", "are", "some", "sample", "strings", "to", "be", "sorted")
print(v[order(-nchar(v), tolower(v))])</langsyntaxhighlight>
 
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">
<lang Racket>
#lang racket
 
Line 1,618 ⟶ 3,669:
(sort2 '("Some" "pile" "of" "words"))
;; -> '("words" "pile" "Some" "of")
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)<br>
Primary sort by length of string, then break ties by sorting alphabetically (ignoring case).
<syntaxhighlight lang="raku" line>my @strings = <Here are some sample strings to be sorted>;
put @strings.sort:{.chars, .lc};
put sort -> $x { $x.chars, $x.lc }, @strings;</syntaxhighlight>
{{out}}
<pre>be to are Here some sample sorted strings
be to are Here some sample sorted strings
</pre>
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program sorts a (stemmed) array using the merge-sort method. */
/* using mycmp function for the sort order */
/**********************************************************************
Line 1,750 ⟶ 3,812:
Otherwise res=0
End
RETURN res</langsyntaxhighlight>
{{out}}
Output:
<pre>
element 1 before sort: ---The seven deadly sins---
Line 1,775 ⟶ 3,837:
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
load "stdlib.ring"
 
sList = newlist(8, 2)
aList = ["Here", "are", "some", "sample", "strings", "to", "be", "sorted"]
ind = len(aList)
 
for n = 1 to ind
sList[n] [1] = aList[n]
sList[n] [2] = len(aList[n])
next
 
nList = sortFirstSecond(sList, 2)
oList = newlist(8, 2)
count = 0
 
for n = len(nList) to 1 step -1
count = count + 1
oList[count] [1] = nList[n] [1]
oList[count] [2] = nList[n] [2]
next
 
for n = 1 to len(oList) - 1
temp1 = oList[n] [1]
temp2 = oList[n+1] [1]
if (oList[n] [2] = oList[n+1] [2]) and (strcmp(temp1, temp2) > 0)
temp = oList[n] [1]
oList[n] [1] = oList[n+1] [1]
oList[n+1] [1] = temp
ok
next
 
for n = 1 to len(oList)
see oList[n] [1] + nl
next
</syntaxhighlight>
Output:
<pre>
strings
sample
sorted
Here
some
are
be
to
</pre>
 
=={{header|Ruby}}==
Since Ruby 1.8.6 Enumerables have a "sort_by" method, taking a key block, which is more efficient than a comparator. We can take advantage of the fact that Arrays are ordered first by the first element, then by the second, etc., to perform a sort on multiple criteria.
 
<langsyntaxhighlight lang="ruby">words = %w(Here are some sample strings to be sorted)
p words.sort_by {|word| [-word.size, word.downcase]}</langsyntaxhighlight>
 
To technically comply with this task, we can also use an actual comparator block which will be called every time members of the original list are to be compared.
<langsyntaxhighlight lang="ruby">p words.sort {|a, b| d = b.size <=> a.size
d != 0 ? d : a.upcase <=> b.upcase}</langsyntaxhighlight>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
fn main() {
let mut words = ["Here", "are", "some", "sample", "strings", "to", "be", "sorted"];
words.sort_by(|l, r| Ord::cmp(&r.len(), &l.len()).then(Ord::cmp(l, r)));
println!("{:?}", words);
}
</syntaxhighlight>
 
=={{header|Sather}}==
<langsyntaxhighlight lang="sather">class MAIN is
 
custom_comp(a, b:STR):BOOL is
Line 1,802 ⟶ 3,920:
loop #OUT + s.elt! + "\n"; end;
end;
end;</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">List("Here", "are", "some", "sample", "strings", "to", "be", "sorted").sortWith{(a,b) =>
val cmp=a.size-b.size
(if (cmp==0) -a.compareTo(b) else cmp) > 0
}</langsyntaxhighlight>
{{out}}
<pre>List(strings, sample, sorted, Here, some, are, be, to)</pre>
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">(use srfi-13);;Syntax for module inclusion depends on implementation,
;;a sort function may be predefined, or available through srfi 95
;;as does the presence of a sort function.
(define (mypred? a b)
(let ((len-a (string-length a))
Line 1,822 ⟶ 3,940:
(> len-a len-b))))
 
(sort '("sorted" "here" "strings" "sample" "Some" "are" "be" "to") mypred?) </langsyntaxhighlight>
{{out}}
Sample output:
<langsyntaxhighlight lang="scheme">("strings" "sample" "sorted" "here" "Some" "are" "be" "to")</langsyntaxhighlight>
 
 
=== An alternative solution: ===
{{works with|Gauche Scheme}}
 
<syntaxhighlight lang="scheme">(define strings '(
"This" "Is" "A" "Set" "Of" "Strings" "To" "Sort" "duplicated"
"this" "is" "a" "set" "of" "strings" "to" "sort" "duplicated"))
 
(print
(sort strings
(lambda two
(define sizes (map string-length two))
(if (apply = sizes)
(apply string-ci<? two)
(apply > sizes)))))
</syntaxhighlight>
{{out}}
<pre>
(duplicated duplicated Strings strings Sort sort This this Set set Is is
Of of To to A a)
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func mycmp(a, b) { (b.len <=> a.len) || (a.lc <=> b.lc) };
var strings = %w(Here are some sample strings to be sorted);
var sorted = strings.sort(mycmp);</syntaxhighlight>
 
=={{header|Slate}}==
<langsyntaxhighlight lang="slate">define: #words -> #('here' 'are' 'some' 'sample' 'strings' 'to' 'sort' 'since' 'this' 'exercise' 'is' 'not' 'really' 'all' 'that' 'dumb' '(sorry)').
words sortBy: [| :first :second | (first lexicographicallyCompare: second) isNegative]</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
<langsyntaxhighlight lang="smalltalk">#('here' 'are' 'some' 'sample' 'strings' 'to' 'sort' 'since' 'this' 'exercise' 'is' 'not' 'really' 'all' 'that' 'dumb' '(sorry)' ) asSortedCollection
sortBlock:
[:first :second | (second size = first size)
ifFalse: [second size < first size]
ifTrue: [first < second]]</langsyntaxhighlight>
the above creates a sorted collection;
an inplace sort of arrayed collections is done with eg.:
<syntaxhighlight lang="smalltalk">#('here' 'are' 'some' 'sample' 'strings')
sort:[:a :b | a reversed < b reversed]</syntaxhighlight>
 
=={{header|Standard ML}}==
List:
{{works with|SML/NJ}}
<langsyntaxhighlight lang="sml">fun mygt (s1, s2) =
if size s1 <> size s2 then
size s2 > size s1
else
String.map Char.toLower s1 > String.map Char.toLower s2</langsyntaxhighlight>
 
<langsyntaxhighlight lang="sml">- val strings = ["Here", "are", "some", "sample", "strings", "to", "be", "sorted"];
val strings = ["Here","are","some","sample","strings","to","be","sorted"]
: string list
- ListMergeSort.sort mygt strings;
val it = ["strings","sample","sorted","Here","some","are","be","to"]
: string list</langsyntaxhighlight>
 
Array:
{{works with|SML/NJ}}
<langsyntaxhighlight lang="sml">fun mycmp (s1, s2) =
if size s1 <> size s2 then
Int.compare (size s2, size s1)
else
String.compare (String.map Char.toLower s1, String.map Char.toLower s2)</langsyntaxhighlight>
 
<langsyntaxhighlight lang="sml">- val strings = Array.fromList ["Here", "are", "some", "sample", "strings", "to", "be", "sorted"];
val strings = [|"Here","are","some","sample","strings","to","be","sorted"|]
: string array
Line 1,867 ⟶ 4,017:
- strings;
val it = [|"strings","sample","sorted","Here","some","are","be","to"|]
: string array</langsyntaxhighlight>
 
=={{header|Swift}}==
{{works with|Swift|2.x+}}
<syntaxhighlight lang="swift">import Foundation
 
var list = ["this",
"is",
"a",
"set",
"of",
"strings",
"to",
"sort",
"This",
"Is",
"A",
"Set",
"Of",
"Strings",
"To",
"Sort"]
 
list.sortInPlace {lhs, rhs in
let lhsCount = lhs.characters.count
let rhsCount = rhs.characters.count
let result = rhsCount - lhsCount
if result == 0 {
return lhs.lowercaseString > rhs.lowercaseString
}
return lhsCount > rhsCount
}</syntaxhighlight>
{{works with|Swift|1.2}}
<syntaxhighlight lang="swift">import Foundation
 
var list = ["this",
"is",
"a",
"set",
"of",
"strings",
"to",
"sort",
"This",
"Is",
"A",
"Set",
"Of",
"Strings",
"To",
"Sort"]
 
sort(&list) {lhs, rhs in
let lhsCount = count(lhs)
let rhsCount = count(rhs)
let result = rhsCount - lhsCount
if result == 0 {
return lhs.lowercaseString > rhs.lowercaseString
}
return lhsCount > rhsCount
}</syntaxhighlight>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc sorter {a b} {
set la [string length $a]
set lb [string length $b]
Line 1,882 ⟶ 4,096:
 
set strings {here are Some sample strings to be sorted}
lsort -command sorter $strings ;# ==> strings sample sorted here Some are be to</langsyntaxhighlight>
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
setofstrings="this is a set of strings to sort This Is A Set Of Strings To Sort"
Line 1,898 ⟶ 4,113:
PRINT "2. setofstrings sorted"
*{sorted}
</syntaxhighlight>
</lang>
{{out}}
Output:
<pre style='height:30ex;overflow:scroll'>
1. setofstrings unsorted
Line 1,938 ⟶ 4,153:
 
=={{header|Ursala}}==
A standard library function, psort, takes a list of binary relational predicates and returns a function that uses them in order of decreasing priority to perform a sort. The less or equal length predicate (leql) and lexically less or equal predicate (lleq) are also standard library functions. This task is therefore easily dispatched as shown.
The less or equal length predicate (leql) and lexically less or equal predicate (lleq) are also standard library functions. This task is therefore easily dispatched as shown.
 
<langsyntaxhighlight Ursalalang="ursala">#import std
#show+
 
data = <'this','is','a','list','of','strings','to','be','sorted'>
 
example = psort<not leql,lleq+ ~* ~&K31K30piK26 letters> data</langsyntaxhighlight>
The lleq library function is case sensitive, so it is composed with a function to convert the words to lower case on the fly (without destructively modifying them) in order to meet the task requirement of case insensitivity.
 
{{out}}
output:
strings
sorted
Line 1,961 ⟶ 4,177:
=={{header|Visual Basic .NET}}==
 
<langsyntaxhighlight lang="vbnet">Imports System
 
Module Sorting_Using_a_Custom_Comparator
Line 1,978 ⟶ 4,194:
Array.Sort(strings, New Comparison(Of String)(AddressOf CustomComparator))
End Sub
End Module</langsyntaxhighlight>
 
=={{header|Wren}}==
{{libheader|Wren-sort}}
<syntaxhighlight lang="wren">import "./sort" for Cmp, Sort
 
var cmp = Fn.new { |s, t|
if (s.count < t.count) return 1
if (s.count > t.count) return -1
return Cmp.insensitive.call(s, t)
}
 
var strings = ["Here", "are", "some", "sample", "strings", "to", "be", "sorted"]
System.print("Unsorted: %(strings)")
Sort.insertion(strings, cmp)
System.print("Sorted : %(strings)")</syntaxhighlight>
 
{{out}}
<pre>
Unsorted: [Here, are, some, sample, strings, to, be, sorted]
Sorted : [strings, sample, sorted, Here, some, are, be, to]
</pre>
 
=={{header|Zig}}==
'''Works with:''' 0.11.x, 0.12.0-dev.1390+94cee4fb2
 
For 0.10.x, replace std.mem.sort with std.sort.sort .
 
<syntaxhighlight lang="zig">const std = @import("std");
 
/// Sort by descending length and ascending lexicographical order.
/// If true, element will remain on it's place.
fn lessThanFn(context: void, left: []const u8, right: []const u8) bool {
_ = context;
// Sort by descending length
switch (std.math.order(left.len, right.len)) {
.lt => return false,
.eq => {},
.gt => return true,
}
 
// If length is equal, sort by ascending lexicographical order
return switch (std.ascii.orderIgnoreCase(left, right)) {
.lt => true,
.eq => false,
.gt => false,
};
}
 
pub fn main() void {
var words = [_][]const u8{ "Here", "are", "some", "sample", "strings", "to", "be", "sorted" };
 
std.debug.print("Before: [ ", .{});
for (words) |word| {
std.debug.print("\"{s}\" ", .{word});
}
std.debug.print("]\n", .{});
 
std.mem.sort([]const u8, &words, {}, lessThanFn);
 
std.debug.print("After: [ ", .{});
for (words) |word| {
std.debug.print("\"{s}\" ", .{word});
}
std.debug.print("]\n", .{});
}</syntaxhighlight>
 
{{out}}
<pre>
Before: [ "Here" "are" "some" "sample" "strings" "to" "be" "sorted" ]
After: [ "strings" "sample" "sorted" "Here" "some" "are" "be" "to" ]
</pre>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">s:=T("Cat","apple","Adam","zero","Xmas","quit","Level","add","Actor","base","butter");
r:=s.sort(fcn(a,b){ an:=a.len();bn:=b.len();
an,bn := if(an==bn)(a.toLowerlen() < ,b.toLowerlen()); else (an > bn)});
if(an==bn)(a.toLower() < b.toLower()) else (an > bn)
r.apply2(Console.println);</lang>
});
r.pump(Console.println);</syntaxhighlight>
{{out}}
<pre>
9,485

edits