我正在尝试根据给定的键值对哈希数组进行排序,并将首先在数组顶部返回该值,然后是剩余数据。
示例是:
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'}]
有谁知道该怎么做?