Sort stability: Difference between revisions

→‎{{header|Lasso}}: Adding Lasso explanation of sort stability
(Added Erlang)
(→‎{{header|Lasso}}: Adding Lasso explanation of sort stability)
Line 327:
<pre>UK,London,US,New York,US,Birmingham,UK,Birmingham
UK,Birmingham,US,Birmingham,UK,London,US,New York</pre>
 
 
 
=={{header|Lasso}}==
Arrays can be sorted in two “built in" ways in Lasso:
 
<lang Lasso>//Single param array:
array->sort
 
//An array of pairs, order by the right hand element of the pair:
with i in array order by #i->second do => { … }
 
//The array can also be ordered by multiple values:
with i in array order by #i->second, #i->first do => { … } </lang>
 
Sorting of arrays by either method uses “Qucksort” and is therefore unstable. A simulation of increasing sort stability would be introduced with additional params such as the example of ordering by the second then the first pair values in the example above - but would not be guaranteed stable.
 
* Note this explanation of sorting does not cover ordering by properties of complex objects, which is also possible using query expressions.
 
Sort by second value only:
<lang Lasso>local(a = array('UK'='London','US'='New York','US'='Birmingham','UK'='Birmingham'))
with i in #a order by #i->second do => {^ #i->first+' - '+#i->second+'\r' ^}</lang>
{{out}}
<pre>US - Birmingham
UK - Birmingham
UK - London
US - New York</pre>
 
Sort by second then by first:
<lang Lasso>local(a = array('UK'='London','US'='New York','US'='Birmingham','UK'='Birmingham'))
with i in #a order by #i->second, #i->first do => {^ #i->first+' - '+#i->second+'\r' ^}</lang>
{{out}}
<pre>UK - Birmingham
US - Birmingham
UK - London
US - New York</pre>
 
=={{header|Lua}}==
140

edits