我有散列的大規模陣列,我需要通過:job_num
數值其通過:job_num
排序之後訂購我需要:date
我將如何對一個哈希值數組進行排序,然後再對它進行排序?
array = [
{job_num: 1236, date: Date.parse('2016/10/19')},
{job_num: 1234, date: Date.parse('2016/10/17')},
{job_num: 1236, date: Date.parse('2016/10/20')},
{job_num: 1233, date: Date.parse('2016/10/18')}
]
# => [{:job_num=>1236, :date=>#<Date: 2016-10-19 ((2457681j,0s,0n),+0s,2299161j)>}, {:job_num=>1234, :date=>#<Date: 2016-10-17 ((2457679j,0s,0n),+0s,2299161j)>}, {:job_num=>1236, :date=>#<Date: 2016-10-20 ((2457682j,0s,0n),+0s,2299161j)>}, {:job_num=>1233, :date=>#<Date: 2016-10-18 ((2457680j,0s,0n),+0s,2299161j)>}]
我需要通過:job_num
array.sort! { |x,y| x[:job_num] <=> y[:job_num] }
# => [{:job_num=>1233, :date=>#<Date: 2016-10-18 ((2457680j,0s,0n),+0s,2299161j)>}, {:job_num=>1234, :date=>#<Date: 2016-10-17 ((2457679j,0s,0n),+0s,2299161j)>}, {:job_num=>1236, :date=>#<Date: 2016-10-20 ((2457682j,0s,0n),+0s,2299161j)>}, {:job_num=>1236, :date=>#<Date: 2016-10-19 ((2457681j,0s,0n),+0s,2299161j)>}]
然後我需要它的日期由工號排序後進行排序,就應該結束了,數組看起來像這樣如果一切順利:
array = [
{job_num: 1233, date: '2016/10/18'},
{job_num: 1234, date: '2016/10/17'},
{job_num: 1236, date: '2016/10/19'},
{job_num: 1236, date: '2016/10/20'}
]
(I離開的時間解析,因爲其僅是示例的最後陣列和無關的邏輯中)
你可以發佈一些你的嘗試代碼嗎? – roelofs