2012-07-27 60 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