2012-01-15 89 views
2

我有三个类似的json对象。现在我想加入整个列表,然后根据其中一个索引对完整列表进行排序。这里,回到原来的对象描述添加两个json对象并排序

对象1

[{"link":"#","prod":"Fundamentals Of Software Engineering","price":" Rs. 200 "},{"link":"##","prod":"Real-Time Systems: Theory And Practice","price":" Rs. 394 "}] 

对象2

[{"link":"#","prod":"Fundamentals Of Software Engineering","price":" Rs. 200 "},{"link":"##","prod":"Real-Time Systems: Theory And Practice","price":" Rs. 394 "}] 

对象3

[{"link":"#","prod":"Fundamentals Of Software Engineering","price":" Rs. 200 "},{"link":"##","prod":"Real-Time Systems: Theory And Practice","price":" Rs. 394 "}] 

有一次,我加入他们所有的人,我想将整个数组(新的)w.r.t排序到价格指数。

任何暗示这将不胜感激。 谢谢:)

+0

三个对象不会有相同的数据 – 2012-01-15 10:50:38

+1

你卡在哪里? [如何解析JSON](http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript),[如何连接数组](http://stackoverflow.com/questions/5080028 /什么是最有效的方式连接n数组在JavaScript中),[如何排序对象数组](http://stackoverflow.com/questions/1129216/sorting- object-in-an-array-by-a-field-value-in-javascript/1129270#1129270)还是别的? – 2012-01-15 10:53:22

+0

我无法连接它们 – 2012-01-15 10:53:53

回答

2

如果Object1, Object2和Object3是JSON字符串,使用eval函数将其转换为Javascript对象。

然后使用concat方法合并它们。 http://www.w3schools.com/jsref/jsref_concat_array.asp

var mergedArray = arr1.concat(arr2, arr3); 

然后排序使用JavaScript数组sort方法。 REF:http://www.w3schools.com/jsref/jsref_sort.asp REF:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort

var sorted = mergedArray.sort(function(a, b){ 

    // This function is used by sort method for sorting process 
    // This function gets called with parameters a,b which are the elements in array (here product objects from your JSON) 
    // if this function returns value < 0 a is placed before b 
    // if this function returns 0 nothing is changed 
    // if this function returns value > 0 b is placed before a 
    // a.price.replace("Rs.", "") removes Rs. from price string. so "Rs. 200" becomes 200 
    // parseFloat(a.price.replace("Rs.", "")) makes it number. "200" becomes 200. "200.50" becomes 200.5 
    // priceA - priceB returns -ve value if priceA < priceB, 0 if priceA = priceB, +ve value if priceA > priceB. 

    var priceA = parseFloat(a.price.replace("Rs.", "")); 
    var priceB = parseFloat(b.price.replace("Rs.", "")); 
    return priceA - priceB; 



}); 

使用return priceB - priceA;的降序排列。 jsfiddle:http://jsfiddle.net/diode/FzzHz/

+0

你能解释一下最后一部分吗?合并数组后,我没有得到这个代码如何排序它们。请解释一下 – 2012-01-15 11:39:57

+0

我已经用解释更新了答案。请检查。 – Diode 2012-01-15 12:00:58

+0

非常感谢。我得到了一切:D – 2012-01-15 16:56:09

1

将它们转换为对象,并this应该做的伎俩

1

你可以使用concat方法来连接的阵列:

var result = arr1.concat(arr2, arr3); 

,然后你可以得到的数组进行排序。

让我们编写的排序功能,将使用prod特性对它们进行排序(你可以用任何你想要的属性对它们进行排序):

function SortByProd(a, b) { 
    var aProd = a.prod.toLowerCase(); 
    var bProd = b.prod.toLowerCase(); 
    return ((aProd < bProd) ? -1 : ((aProd > bProd) ? 1 : 0)); 
} 

,然后排序:

result.sort(SortByProd); 
+0

如果他使用jQuery ... – 2012-01-15 10:57:08

+0

@FelixKling,oops,正确。固定。 – 2012-01-15 10:59:51