Sort stability: Difference between revisions

no edit summary
(→‎{{header|OpenEdge/Progress}}: Marked incorrect as a particular sort might give a particular result whilst saying nothing about the stability '''in general''')
No edit summary
Line 292:
]
sort/skip/compare blk 2 func [a b] [either a < b [-1] [either a > b [1] [0]]]</lang>
 
=={{header|REXX}}==
Open Object Rexx provides sort methods (<code>sort</code> and <code>sortWith(comparator)</code>) for its collection classes. By default these sort methods are implemented via an unstable <em>Quicksort</em> algorithm but the language does provide stable sorting methods (<code>stableSort</code> and <code>stableSortWith(comparator)</code>) implemented via a stable <em>Mergesort</em> algorithm.
<lang REXX>/* Rexx */
Do
cities = .array~of('UK London', 'US New York', 'US Birmingham', 'UK Birmingham',)
 
Say; Say 'Original table'
Call display cities
 
Say; Say 'Unstable sort on city'
sorted = cities~copy
sorted~sortWith(.ColumnComparator~new(4, 20))
Call display sorted
 
Say; Say 'Stable sort on city'
sorted = cities~copy
sorted~stableSortWith(.ColumnComparator~new(4, 20))
Call display sorted
 
Say; Say 'Unstable sort on country'
sorted = cities~copy
sorted~sortWith(.ColumnComparator~new(1, 2))
Call display sorted
 
Say; Say 'Stable sort on country'
sorted = cities~copy
sorted~stableSortWith(.ColumnComparator~new(1, 2))
Call display sorted
 
Return
End
Exit
 
display: Procedure
Do
Use arg CT
 
Say '-'~copies(80)
Loop c_ over CT
Say c_
End c_
 
Return
End
Exit
</lang>
;Output
<pre>
Original table
--------------------------------------------------------------------------------
UK London
US New York
US Birmingham
UK Birmingham
 
Unstable sort on city
--------------------------------------------------------------------------------
UK Birmingham
US Birmingham
UK London
US New York
 
Stable sort on city
--------------------------------------------------------------------------------
US Birmingham
UK Birmingham
UK London
US New York
 
Unstable sort on country
--------------------------------------------------------------------------------
UK London
UK Birmingham
US Birmingham
US New York
 
Stable sort on country
--------------------------------------------------------------------------------
UK London
UK Birmingham
US New York
US Birmingham
</pre>
 
=={{header|Ruby}}==
Anonymous user