Anonymous user
Sort stability: Difference between revisions
→{{header|Ruby}}: Distinguish JRuby from MRI.
(→{{header|Ruby}}: Distinguish JRuby from MRI.) |
|||
Line 558:
=={{header|Ruby}}==
Ruby's built-in sort methods
{{works with|MRI|1.8.7, 1.9.2}}
ary.sort {|a,b| a[1] <=> b[1]}▼
<lang ruby>ary = [["UK", "London"],
["US", "New York"],
["US", "Birmingham"],
["UK", "Birmingham"]]
▲p ary.sort {|a,b| a[1] <=> b[1]}
# MRI reverses the Birminghams:
# => [["UK", "Birmingham"], ["US", "Birmingham"], ["UK", "London"], ["US", "New York"]]</lang>
Other implementations of Ruby might differ. Old versions of [[JRuby]] used java.util.Arrays.sort, which is a stable sort, but is slower than MRI. To increase performance, JRuby switched to quicksort, which is not stable. [http://jira.codehaus.org/browse/JRUBY-2198 (3)]
<!--
To code a stable sort[[http://codesnippets.joyent.com/posts/show/1698 2]]:
|