Sort using a custom comparator

Sort using a custom comparator
You are encouraged to solve this task according to the task description, using any language you may know.

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.

Unsorted
********
Here
are
some
sample
strings
to
be
sorted
Descending Length
*****************
strings
sample
sorted
Here
some
are
be
to
Ascending order
***************
are
be
Here
sample
some
sorted
strings
to

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.

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 sort function is destructive; for lists you should not use the original list afterwards, you should only use the return value.

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

Fortran does not have builtin to sort arrays (of numbers or strings), with or without custom comparator; so we need modifying e.g. this code in order to handle strings and to accept a custom comparator.

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

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.

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.

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.

words = %w(Here are some sample strings to be sorted)p words.sort_by{|word|[-word.size, word.downcase]}

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.

$$ MODE TUSCRIPTsetofstrings="this is a set of strings to sort This Is A Set Of Strings To Sort"unsorted=SPLIT (setofstrings,": :")PRINT "1. setofstrings unsorted"index=""LOOP l=unsortedPRINT llength=LENGTH (l),index=APPEND(index,length)ENDLOOPindex =DIGIT_INDEX (index)sorted=INDEX_SORT (unsorted,index)PRINT "2. setofstrings sorted"*{sorted}

Output:

1. setofstrings unsorted
this
is
a
set
of
strings
to
sort
This
Is
A
Set
Of
Strings
To
Sort
2. setofstrings sorted
a
A
is
of
to
Is
Of
To
set
Set
this
sort
This
Sort
strings
Strings

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.

#import std#show+

data = <'this','is','a','list','of','strings','to','be','sorted'>

example = psort<not leql,lleq+ ~* ~&K31K30piK26 letters> data

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.