add Enumerable#sort_by

parent 027d6407
......@@ -161,6 +161,27 @@ module Enumerable
h
end
##
# call-seq:
# enum.sort_by { |obj| block } -> array
#
# Sorts <i>enum</i> using a set of keys generated by mapping the
# values in <i>enum</i> through the given block.
def sort_by(&block)
ary = []
orig = []
self.each_with_index{|e, i|
orig.push(e)
ary.push([block.call(e), i])
}
if ary.size > 1
__sort_sub__(ary, ::Array.new(ary.size), 0, 0, ary.size - 1) do |a,b|
a <=> b
end
end
ary.collect{|e,i| orig[i]}
end
NONE = Object.new
##
# call-seq:
......
......@@ -390,7 +390,7 @@ module Enumerable
def sort(&block)
ary = []
self.each{|*val| ary.push(val.__svalue)}
unless ary.empty?
if ary.size > 1
__sort_sub__(ary, ::Array.new(ary.size), 0, 0, ary.size - 1, &block)
end
ary
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment