Jump to content

Sort stability: Difference between revisions

→‎{{header|Oz}}: Added example
No edit summary
(→‎{{header|Oz}}: Added example)
Line 74:
Oz' [http://www.mozart-oz.org/home/doc/base/list.html#label295 Sort] function is not guaranteed to be stable in the documentation.
 
However, internally it uses mergesort[[Merge sort]] and in practice ''is'' stable '''if''' a reflexive ordering is used, e.g. <code>Value.'=<'</code> or <code>Value.'>='</code>.
 
Example:
<lang oz>declare
Cities = ['UK'#'London'
'US'#'New York'
'US'#'Birmingham'
'UK'#'Birmingham']
in
%% sort by city; stable because '=<' is reflexiv
{Show {Sort Cities fun {$ A B} A.2 =< B.2 end}}
 
%% sort by country; NOT stable because '<' is not reflexiv
{Show {Sort Cities fun {$ A B} A.1 < B.1 end}}</lang>
 
=={{header|Perl}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.