I have an array, each of whose elements is a hash with three key/value pairs:
我有一个数组,每个元素都是一个散列,有三个键/值对:
:phone => "2130001111", :zip => "12345", :city => "sometown"
I'd like to sort the data by zip
so all the phone
s in the same area are together. Does Ruby have an easy way to do that? Can will_paginate
paginate data in an array?
我想把数据按zip分类,以便同一地区的所有电话都在一起。Ruby有一种简单的方法吗?will_paginate在数组中分页数据吗?
6 个解决方案
#1
296
Simples:
样品:
array_of_hashes.sort_by { |hsh| hsh[:zip] }
Note:
注意:
When using sort_by
you need to assign the result to a new variable: array_of_hashes = array_of_hashes.sort_by{}
otherwise you can use the "bang" method to modify in place: array_of_hashes.sort_by!{}
在使用sort_by时,需要将结果分配给一个新变量:array_of_hashes = array_of_hashes。否则,您可以使用“bang”方法就地进行修改:array_of_hashes.sort_by!
#2
16
sorted = dataarray.sort {|a,b| a[:zip] <=> b[:zip]}
#3
5
Use the bang to modify in place the array:
使用bang对数组进行修改:
array_of_hashes.sort_by!(&:zip)
Or re-assign it:
或重新分配:
array_of_hashes = array_of_hashes.sort_by(&:zip)
Note that sort_by method will sort by ascending order.
注意sort_by方法将按升序排序。
If you need to sort with descending order you could do something like this:
如果你需要按照降序排序你可以这样做:
array_of_hashes.sort_by!(&:zip).reverse!
or
或
array_of_hashes = array_of_hashes.sort_by(&:zip).reverse
#4
4
If you want to paginate for data in array you should require 'will_paginate/array' in your controller
如果要对数组中的数据进行分页,应该在控制器中要求“will_paginate/array”。
#5
0
nil is not comparable so to avoid meeting it
nil是无法与之相比的,所以避免遇到它。
sorted = dataarray.sort do |a,b|
if a[:zip] == nil
-1 # nil gets to the start, swap if you need otherwise
elsif b[:zip] == nil
1
else
a[:zip] <=> b[:zip]
end
end
#6
0
If you have Nested Hash (Hash inside a hash format) as Array elements (a structure like the following) and want to sort it by key (date here)
如果您将嵌套散列(哈希格式中的散列)作为数组元素(如下所示的结构)并希望按键(这里的日期)对其进行排序
data = [
{
"2018-11-13": {
"avg_score": 4,
"avg_duration": 29.24
}
},
{
"2017-03-13": {
"avg_score": 4,
"avg_duration": 40.24
}
},
{
"2018-03-13": {
"avg_score": 4,
"avg_duration": 39.24
}
}
]
Use Array 'sort_by' method as
使用数组'sort_by'方法as
data.sort_by { |element| element.keys.first }
#1
296
Simples:
样品:
array_of_hashes.sort_by { |hsh| hsh[:zip] }
Note:
注意:
When using sort_by
you need to assign the result to a new variable: array_of_hashes = array_of_hashes.sort_by{}
otherwise you can use the "bang" method to modify in place: array_of_hashes.sort_by!{}
在使用sort_by时,需要将结果分配给一个新变量:array_of_hashes = array_of_hashes。否则,您可以使用“bang”方法就地进行修改:array_of_hashes.sort_by!
#2
16
sorted = dataarray.sort {|a,b| a[:zip] <=> b[:zip]}
#3
5
Use the bang to modify in place the array:
使用bang对数组进行修改:
array_of_hashes.sort_by!(&:zip)
Or re-assign it:
或重新分配:
array_of_hashes = array_of_hashes.sort_by(&:zip)
Note that sort_by method will sort by ascending order.
注意sort_by方法将按升序排序。
If you need to sort with descending order you could do something like this:
如果你需要按照降序排序你可以这样做:
array_of_hashes.sort_by!(&:zip).reverse!
or
或
array_of_hashes = array_of_hashes.sort_by(&:zip).reverse
#4
4
If you want to paginate for data in array you should require 'will_paginate/array' in your controller
如果要对数组中的数据进行分页,应该在控制器中要求“will_paginate/array”。
#5
0
nil is not comparable so to avoid meeting it
nil是无法与之相比的,所以避免遇到它。
sorted = dataarray.sort do |a,b|
if a[:zip] == nil
-1 # nil gets to the start, swap if you need otherwise
elsif b[:zip] == nil
1
else
a[:zip] <=> b[:zip]
end
end
#6
0
If you have Nested Hash (Hash inside a hash format) as Array elements (a structure like the following) and want to sort it by key (date here)
如果您将嵌套散列(哈希格式中的散列)作为数组元素(如下所示的结构)并希望按键(这里的日期)对其进行排序
data = [
{
"2018-11-13": {
"avg_score": 4,
"avg_duration": 29.24
}
},
{
"2017-03-13": {
"avg_score": 4,
"avg_duration": 40.24
}
},
{
"2018-03-13": {
"avg_score": 4,
"avg_duration": 39.24
}
}
]
Use Array 'sort_by' method as
使用数组'sort_by'方法as
data.sort_by { |element| element.keys.first }