arr = [
[0, "Moving Companies", 10],
[0, "ab-thera-sensa", 5],
[0, "belt-center", 16],
[0, "isabel", 3],
[0, "kreatio", 2],
[0, "service1", 7],
[0, "sorbion-sachet-multi-star", 6],
[0, "sss", 15],
[0, "telecom-service-industry", 14],
[1, " AbsoPad", 13],
[1, "telecom-service", 8],
[2, "cutisorb-ultra", 12],
[2, "sorbion-contact", 11],
[2, "sorbion-sachet-multi-star", 9]
]
假設這是我的數組,現在我想按照第一個元素的降序對它進行排序。我可以做一個arr.sort.reverse
但現在的問題開始在Ruby on Rails中排序數組
我得到的數組:
[
[2, "sorbion-sachet-multi-star", 9],
[2, "sorbion-contact", 11],
[2, "cutisorb-ultra", 12],
[1, "telecom-service", 8],
[1, " AbsoPad", 13],
[0, "telecom-service-industry", 14],
[0, "sss", 15], [0, "sorbion-sachet-multi-star", 6],
[0, "service1", 7],
[0, "kreatio", 2],
[0, "isabel", 3],
[0, "belt-center", 16],
[0, "ab-thera-sensa", 5],
[0, "Moving Companies", 10]
]
現在數組應該按升序排列的第二個元素的基礎上進行排序。
這怎麼能實現?
結果應該是這樣的:
[
[2, "cutisorb-ultra", 12],
[2, "sorbion-contact", 11],
[2, "sorbion-sachet-multi-star", 9],
[1,.......]
]
你是正確的+1。這個編輯對我來說很重要,因爲OP在尋找什麼。 –
謝謝,你能解釋一下你在那裏做了什麼。 – aelor