如何按值对哈希数组进行排序?

时间:2012-08-03 19:26:05

标签: ruby arrays sorting hash

我试图根据给定的键值对哈希数组进行排序,并首先在数组顶部返回该值,然后是剩余数据。

示例是:

students = [{name: "John Doe", age: 16, adviser: "Mrs. Robinson"},
            {name: "John Smith", age: 18, adviser: "Mrs. Williams"},
            {name: "Michael Rodriguez", age: 17, adviser: "Mr. Lee"}]

def sort_by_adviser(data, name)
  ...
end

> sort_by_adviser(students, "Mr. Lee")
=> [{name: "Michael Rodriguez", age: 17, adviser: "Mr. Lee"},
    {name: "John Doe", age: 16, adviser: "Mrs. Robinson"},
    {name: "John Smith", age: 18, adviser: "Mrs. Williams"}]

> sort_by_adviser(students, "Mrs. Williams")
=> [{name: "John Smith", age: 18, adviser: "Mrs. Williams"},
    {name: "Michael Rodriguez", age: 17, adviser: "Mr. Lee"},
    {name: "John Doe", age: 16, adviser: "Mrs. Robinson"}]

此处输出会将顾问的名称放在列表顶部,然后是数组中的其他哈希值。

> sort_by_keyvalue(data, "Z")
=> [{letter: 'Z'},
    {letter: 'A'},
         .
         .
         .
    {letter: 'Y'}]

> sort_by_keyvalue(data, 5)
=> [{number: 5, value: 'value1'},
    {number: 5, value: 'value2'},
    {number: 5, value: 'value3'},
    {number: 9, value: 'value1'},
    {number: 9, value: 'value2'},
    {number: 8, value: 'value1'},
    {number: 8, value: 'value2'},
    {number: 7, value: 'value1'},
    {number: 6, value: 'value1'},
    {number: 4, value: 'value1'},
    {number: 3, value: 'value1'},
    {number: 2, value: 'value1'},
    {number: 1, value: 'value1'},
    {number: 1, value: 'value2'},
    {number: 0, value: 'value1'}]

任何人都知道怎么做?

5 个答案:

答案 0 :(得分:3)

另一种实施:)

def sort_by_adviser(data, name)
    data.each_with_index do |hash,index|
      if hash[:adviser]==name
         data.delete_at index #delete from array
         data.unshift hash
         break
      end
    end
   data
end

 > sort_by_adviser(students, "Mr. Lee")  
 #=> [{:name=>"Michael Rodriguez", :age=>17, :adviser=>"Mr. Lee"}, {:name=>"John Doe", :age=>16, :adviser=>"Mrs. Robinson"}, {:name=>"John Smith", :age=>18, :adviser=>"Mrs. Williams"}] 

答案 1 :(得分:2)

def creamy_sort(key, value, arr)
  top, bottom = arr.partition{|e| e[key] == value }
  top.concat(bottom.sort{|a,b| b[key] <=> a[key]})
end

creamy_sort(:adviser, "Mr. Lee", students)

答案 2 :(得分:1)

你可以这样做:

def sort_by_adviser(data, name)
  data = data.sort{|x,y|x[:adviser] <=> y[:adviser]}
  i = data.index{|h|h[:adviser] = name}
  h = data.delete_at i
  data.unshift h
end

答案 3 :(得分:1)

我有这个解决方案:

students = [{name: "John Doe", age: 16, adviser: "Mrs. Robinson"},
            {name: "John Smith", age: 18, adviser: "Mrs. Williams"},
            {name: "Michael Rodriguez", age: 17, adviser: "Mr. Lee"}]

def sort_by_adviser(data, *name)
  data.sort_by{| entry |    
    [ 
      name.index(entry[:adviser]) || 999,
      entry[:age], entry[:name] #2nd sort criteria
    ]
  }
end

p sort_by_adviser(students, "Mr. Lee")
#[{:name=>"Michael Rodriguez", :age=>17, :adviser=>"Mr. Lee"}, {:name=>"John Doe", :age=>16, :adviser=>"Mrs. Robinson"}, {:name=>"John Smith", :age=>18, :adviser=>"Mrs. Williams"}]

p sort_by_adviser(students, "Mrs. Williams")
# [{:name=>"John Smith", :age=>18, :adviser=>"Mrs. Williams"}, {:name=>"John Doe", :age=>16, :adviser=>"Mrs. Robinson"}, {:name=>"Michael Rodriguez", :age=>17, :adviser=>"Mr. Lee"}]

我不明白,其余条目的排序是什么。

您写道:然后是剩余数据。哈希的订单标准是什么?

我选择了年龄,然后是姓名。但是你可以根据自己的需要调整它。

答案 4 :(得分:1)

def weird_sort(array, key, value)
  return array.sort_by{|d| 2 <=> (d[key] == value).object_id}
end

这是基于红宝石中true.object_id等于2的事实。 一种奇怪的解决方案,这就是为什么它是weird_sort:p它还会混淆其他值排序...所以它只能保证你的价值相等!#/ p>