2012-07-27 59 views
0

我得到我的json響應並解析它,我能夠分別獲取以下模型和標識符(請參閱下文)。 現在,我只需要按照模型對它們進行排序,不區分大小寫。所以,如果我創建一個數組,那麼即使不是不可能,也很難按照模型進行排序,如果我做了散列,我將無法創建重複的鍵(模型)。
那麼排序這個json響應的最佳方法是什麼?謝謝 !!Ruby:如何使用數組或哈希映射對json響應進行排序

this is the model: 1 Series 
    this is the identifier: Convertible 
    this is the model: 1 Series 
    this is the identifier: Coupe 
    this is the model: 1 Series M 
    this is the identifier: Coupe 
    this is the model: 3 Series 
    this is the identifier: Convertible 
    this is the model: 3 Series 
    this is the identifier: Coupe 
    this is the model: 3 Series 
    this is the identifier: Diesel 
    this is the model: 3 Series 
    this is the identifier: Sedan 
    this is the model: 3 Series 
    this is the identifier: Wagon 
    this is the model: ALPINA B7 
    this is the identifier: Sedan 
    this is the model: ActiveHybrid 5 
    this is the identifier: Sedan 
    this is the model: ActiveHybrid 7 
    this is the identifier: Sedan 
    this is the model: ActiveHybrid X6 
    this is the identifier: SUV 
+0

但是你試過了什麼?歡迎! – megas 2012-07-27 19:08:08

回答

0

我會去與在Comparable混合類的對象數組。你只需要實現<=>(又名「宇宙飛船」),然後你可以在你的陣列上調用sort

+0

只能使用「1系列」,「3系列」......不區分大小寫的情況下,如何對[[1系列,可轉換],[「3系列」,「雙門轎跑車」]]進行排序?任何幫助真的很感激。謝謝 – brzspirit 2012-07-30 23:46:14

0

爲什麼數組不可能排序? 這將工作。

[["1 Series","Convertible"],["3 Series","Coupe"]].sort 
+0

什麼是使用循環創建此數組的語法?謝謝!! – brzspirit 2012-07-30 07:11:45

+0

你將如何排序只使用「1系列」,「3系列」......謝謝!我已經有幾天的時間了,所以任何獲得這個釘子的幫助都非常感謝。再次感謝!! – brzspirit 2012-07-30 17:53:57

+0

array.sort_by {| e | e [0]} – 2012-07-31 08:28:55