Sort stability: Difference between revisions

m (added a comma)
(→‎{{header|Groovy}}: new solution)
Line 107:
The sort package documentation makes no mention of stability. Inspection of the source shows multiple sort algorithms, including quicksort. Presumably then sorts are unstable.
 
=={{header|Groovy}}==
Groovy's [http://groovy.codehaus.org/groovy-jdk/java/util/Collection.html#sort() Collection.sort()], Object[].[http://groovy.codehaus.org/groovy-jdk/java/lang/Object%5b%5d.html#sort() sort()], [http://groovy.codehaus.org/groovy-jdk/java/util/Map.html#sort() Map.sort()], and their various and sundry overloads all use the same stable sort algorithm.
 
Example:
{{trans|Java}}
<lang groovy>def cityList = ['UK London', 'US New York', 'US Birmingham', 'UK Birmingham',].asImmutable()
[
'Sort by city': { city -> city[4..-1] },
'Sort by country': { city -> city[0..3] },
].each{ String label, Closure orderBy ->
println "\n\nBefore ${label}"
cityList.each { println it }
println "\nAfter ${label}"
cityList.sort(false, orderBy).each{ println it }
}</lang>
 
Output:
<pre>Before Sort by city
UK London
US New York
US Birmingham
UK Birmingham
 
After Sort by city
US Birmingham
UK Birmingham
UK London
US New York
 
 
Before Sort by country
UK London
US New York
US Birmingham
UK Birmingham
 
After Sort by country
UK London
UK Birmingham
US New York
US Birmingham</pre>
=={{header|Haskell}}==
Haskell's <tt>sort</tt> and <tt>sortBy</tt> functions are guaranteed stable.[http://www.haskell.org/onlinereport/list.html#sect17.3]
Anonymous user