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)
  ...
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'}]

有谁知道该怎么做?

4

5 回答 5

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"}] 
于 2012-08-03T20:21:25.713 回答
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)
于 2012-08-03T19:49:04.700 回答
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"}]

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

你写道:然后是剩余的数据。哈希的顺序标准是什么?

我选择了年龄,然后是名字。但是您可以根据需要对其进行调整。

于 2012-08-03T19:52:08.137 回答
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
于 2012-08-03T19:47:04.990 回答
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 它也弄乱了其他值排序......所以它只保证你的值相等!

于 2012-08-03T20:03:44.997 回答