So I have an array of hashes:
所以我有一系列哈希:
[{"id":"30","name":"Dave"},
{"id":"57","name":"Mike"},
{"id":"9","name":"Kevin"},
...
{"id":"1","name":"Steve"}]
And I want to sort it by the id
attribute, so that it looks like this:
我想用id属性对它进行排序,所以它看起来像这样:
[{"id":"1","name":"Steve"},
{"id":"2","name":"Walter"},
...
{"id":"60","name":"Chester"}]
I'm assuming I use the sort_by
method but I'm not exactly sure how to do it.
我假设我使用sort_by方法,但我不确定如何做到这一点。
3 个解决方案
#1
16
This should work:
这应该工作:
array.sort_by { |hash| hash['id'].to_i }
In this case, sort_by
is preferred over sort
because it is more efficient. While sort
calls to_i
on every comparison, sort_by
does it once for each element in array and remembers the result.
在这种情况下,sort_by优先于sort,因为它更有效。虽然sort对每次比较调用to_i,但sort_by会对数组中的每个元素执行一次并记住结果。
#2
5
When I see incoming data like that, it's almost always a JSON string. Ruby doesn't automatically understand JSON, nor does it automatically know how to convert it, but Ruby does make it easy for us to convert from/to it:
当我看到这样的传入数据时,它几乎总是一个JSON字符串。 Ruby不会自动理解JSON,也不会自动知道如何转换它,但Ruby确实使我们可以轻松地从/转换它:
require 'json'
json_data = '[{"id":"30","name":"Dave"},
{"id":"57","name":"Mike"},
{"id":"9","name":"Kevin"},
{"id":"1","name":"Steve"}]'
ary = JSON[json_data].sort_by{ |e| e['id'].to_i }
ary
# => [{"id"=>"1", "name"=>"Steve"}, {"id"=>"9", "name"=>"Kevin"}, {"id"=>"30", "name"=>"Dave"}, {"id"=>"57", "name"=>"Mike"}]
The only real trick here is:
这里唯一真正的诀窍是:
JSON[json_data]
A lot of time you'll see people use JSON.parse(json_data)
, but the []
method is smart enough to recognize whether it's getting a String or an array or a hash. If it's a string it tries to parse it assuming it's incoming data. If it's an array or a hash, it converts it to a JSON string for output. The result is, using JSON[...]
simplifies the use of the class and makes it so we don't have to use parse
or to_json
.
很多时候你会看到人们使用JSON.parse(json_data),但[]方法足够聪明,可以识别它是获取String还是数组或哈希。如果它是一个字符串,它会尝试解析它,假设它是传入的数据。如果是数组或散列,则将其转换为JSON字符串以进行输出。结果是,使用JSON [...]简化了类的使用并使其成为我们不必使用parse或to_json。
Otherwise, using sort_by
is preferred over using sort
unless you are directly comparing two simple variables, like integer to integer, string to string or character to character. Once you have to dive into an object, or do some sort of calculation to determine how things compare, then you should use sort_by
. See Wikipedia's article on Schwartzian Transform to understand what's going on under the covers. It's a very powerful technique that can speed up sorting remarkably.
否则,使用sort_by比使用sort更受欢迎,除非您直接比较两个简单变量,如整数到整数,字符串到字符串或字符到字符。一旦你必须深入到一个对象,或做某种计算来确定比较的方式,那么你应该使用sort_by。请参阅Wikipedia关于Schwartzian Transform的文章,以了解幕后发生的事情。这是一种非常强大的技术,可以显着加快排序速度。
#3
2
Your Hash syntax is wrong, if they where symbols then it would look like this:
您的Hash语法是错误的,如果它们在符号中,那么它将如下所示:
data = [
{id:"30", name:"Dave"},
{id:"57", name:"Mike"},
{id:"9", name:"Kevin"},
{id:"1", name:"Steve"}
]
sorted_data = data.sort_by{|x| x[:id].to_i}
Edit: Forgot the to_i, fixed. If the keys are strings the : way of defining a hash does not work, so we need hash-rockets instead:
编辑:忘记to_i,修复。如果键是字符串,那么:定义哈希的方式不起作用,所以我们需要哈希火箭:
data = [{"id"=>"30","name"=>"Dave"},
{"id"=>"57","name"=>"Mike"},
{"id"=>"9","name"=>"Kevin"},
{"id"=>"1","name"=>"Steve"}]
sorted_data = data.sort_by{|x| x['id'].to_i}
#1
16
This should work:
这应该工作:
array.sort_by { |hash| hash['id'].to_i }
In this case, sort_by
is preferred over sort
because it is more efficient. While sort
calls to_i
on every comparison, sort_by
does it once for each element in array and remembers the result.
在这种情况下,sort_by优先于sort,因为它更有效。虽然sort对每次比较调用to_i,但sort_by会对数组中的每个元素执行一次并记住结果。
#2
5
When I see incoming data like that, it's almost always a JSON string. Ruby doesn't automatically understand JSON, nor does it automatically know how to convert it, but Ruby does make it easy for us to convert from/to it:
当我看到这样的传入数据时,它几乎总是一个JSON字符串。 Ruby不会自动理解JSON,也不会自动知道如何转换它,但Ruby确实使我们可以轻松地从/转换它:
require 'json'
json_data = '[{"id":"30","name":"Dave"},
{"id":"57","name":"Mike"},
{"id":"9","name":"Kevin"},
{"id":"1","name":"Steve"}]'
ary = JSON[json_data].sort_by{ |e| e['id'].to_i }
ary
# => [{"id"=>"1", "name"=>"Steve"}, {"id"=>"9", "name"=>"Kevin"}, {"id"=>"30", "name"=>"Dave"}, {"id"=>"57", "name"=>"Mike"}]
The only real trick here is:
这里唯一真正的诀窍是:
JSON[json_data]
A lot of time you'll see people use JSON.parse(json_data)
, but the []
method is smart enough to recognize whether it's getting a String or an array or a hash. If it's a string it tries to parse it assuming it's incoming data. If it's an array or a hash, it converts it to a JSON string for output. The result is, using JSON[...]
simplifies the use of the class and makes it so we don't have to use parse
or to_json
.
很多时候你会看到人们使用JSON.parse(json_data),但[]方法足够聪明,可以识别它是获取String还是数组或哈希。如果它是一个字符串,它会尝试解析它,假设它是传入的数据。如果是数组或散列,则将其转换为JSON字符串以进行输出。结果是,使用JSON [...]简化了类的使用并使其成为我们不必使用parse或to_json。
Otherwise, using sort_by
is preferred over using sort
unless you are directly comparing two simple variables, like integer to integer, string to string or character to character. Once you have to dive into an object, or do some sort of calculation to determine how things compare, then you should use sort_by
. See Wikipedia's article on Schwartzian Transform to understand what's going on under the covers. It's a very powerful technique that can speed up sorting remarkably.
否则,使用sort_by比使用sort更受欢迎,除非您直接比较两个简单变量,如整数到整数,字符串到字符串或字符到字符。一旦你必须深入到一个对象,或做某种计算来确定比较的方式,那么你应该使用sort_by。请参阅Wikipedia关于Schwartzian Transform的文章,以了解幕后发生的事情。这是一种非常强大的技术,可以显着加快排序速度。
#3
2
Your Hash syntax is wrong, if they where symbols then it would look like this:
您的Hash语法是错误的,如果它们在符号中,那么它将如下所示:
data = [
{id:"30", name:"Dave"},
{id:"57", name:"Mike"},
{id:"9", name:"Kevin"},
{id:"1", name:"Steve"}
]
sorted_data = data.sort_by{|x| x[:id].to_i}
Edit: Forgot the to_i, fixed. If the keys are strings the : way of defining a hash does not work, so we need hash-rockets instead:
编辑:忘记to_i,修复。如果键是字符串,那么:定义哈希的方式不起作用,所以我们需要哈希火箭:
data = [{"id"=>"30","name"=>"Dave"},
{"id"=>"57","name"=>"Mike"},
{"id"=>"9","name"=>"Kevin"},
{"id"=>"1","name"=>"Steve"}]
sorted_data = data.sort_by{|x| x['id'].to_i}