使用jquery按价格排序对象

时间:2014-10-06 06:38:45

标签: javascript php jquery

  • 我的要求是根据价格对物品进行分类。基于现金,物品应该被分类。
  • 以下是我安慰并获取并在下面显示的示例数据。

  • 通过安慰full_sky.sort(test);我的数据低于数据。


0: Object
id: 1
test: Array[1]
0: Object
fuldata: "xyz"
pat: "xyz"
img: "6E.gif"
nos: "0 - stop"
show_price: 1
sour: "BLR"
tickettype: "e"
__proto__: Object
length: 1
__proto__: Array[0]
price: Array[1]
0: Object
cash: 44499
total_surcharge: 885
total_tax: 1414
__proto__: Object
length: 1

1: Object
id: 1
test: Array[1]
0: Object
fuldata: "xyz"
pat: "xyz"
img: "6E.gif"
nos: "0 - stop"
show_price: 1
sour: "BLR"
tickettype: "e"
__proto__: Object
length: 1
__proto__: Array[0]
price: Array[1]
0: Object
cash: 2299
total_surcharge: 885
total_tax: 1414
__proto__: Object
length: 1

2: Object
id: 1
test: Array[1]
0: Object
fuldata: "xyz"
pat: "xyz"
img: "6E.gif"
nos: "0 - stop"
show_price: 1
sour: "BLR"
tickettype: "e"
__proto__: Object
length: 1
__proto__: Array[0]
price: Array[1]
0: Object
cash: 3399
total_surcharge: 885
total_tax: 1414
__proto__: Object
length: 1

输出应该是这样的

1: Object
id: 1
test: Array[1]
0: Object
fuldata: "xyz"
pat: "xyz"
img: "6E.gif"
nos: "0 - stop"
show_price: 1
sour: "BLR"
tickettype: "e"
__proto__: Object
length: 1
__proto__: Array[0]
price: Array[1]
0: Object
cash: 2299
total_surcharge: 885
total_tax: 1414
__proto__: Object
length: 1

2: Object
id: 1
test: Array[1]
0: Object
fuldata: "xyz"
pat: "xyz"
img: "6E.gif"
nos: "0 - stop"
show_price: 1
sour: "BLR"
tickettype: "e"
__proto__: Object
length: 1
__proto__: Array[0]
price: Array[1]
0: Object
cash: 3399
total_surcharge: 885
total_tax: 1414
__proto__: Object
length: 1

0: Object
id: 1
test: Array[1]
0: Object
fuldata: "xyz"
pat: "xyz"
img: "6E.gif"
nos: "0 - stop"
show_price: 1
sour: "BLR"
tickettype: "e"
__proto__: Object
length: 1
__proto__: Array[0]
price: Array[1]
0: Object
cash: 44499
total_surcharge: 885
total_tax: 1414
__proto__: Object
length: 1

3 个答案:

答案 0 :(得分:0)

对于这些操作,您可以尝试使用lo-dash库。 (http://lodash.com/docs

请查看文档了解更多信息。 SortBy函数应解决您的问题Lo-Dash SortBy

答案 1 :(得分:0)

Javascript无法确保对象中的订单,因此无法直接对对象进行排序。请参阅this answer以更好地了解原因和相关文档。在您的情况下,我将重新创建一个key-value对列表,其中key作为对象的cash属性,然后对该数组进行排序,直接修改sort函数。

假设您的初始对象数组名为list1,您可以这样做:

 For simplicity:
 list2 = [];
 for (i=0;i<list1.length;i++){ list2.push({'key': list1[i].price.cash,'val' : list1[i]});}

 //list2 would be populated as :
 list2 = [{key:44499, val: Object},..];

 //To sort the list2 based on ascending order of cash

 list2 = list2.sort(function (a, b) {
      return a.key - b.key;
 });


 //In your case,you could accomplish this directly as well doing just :
 list1.sort(function (a, b) {
  return a.cash.price- b.cash.price;
 });

在这里,我已经覆盖了sort函数以稍微处理订单。关于排序函数在javascript中如何工作的relevant SO question可能会有更多的帮助。

答案 2 :(得分:0)

这也可以这样做:

   var myObject= [
          { Cash: 323, Text: "abc" },
          { Cash: 4567, Text: "zxc" },
          { Cash: 83456, Text: "fgg" },
          { Cash: 72, Text: "hjk" },
          { Cash: 1, Text: "tyu" },
          { Cash: 543, Text: "bgt" },
          { Cash: 245, Text: "ljj" },
          { Cash: 68798, Text: "mnu" }
   ];

   // Sort ascending
   myObject.sort(function (a,b){
         if (a.Cash < b.Cash)
            return -1;
         if (a.Cash> b.Cash)
            return 1;
         return 0;
   });

   // Sort descending
   myObject.sort(function (a,b){
         if (a.Cash < b.Cash)
            return 1;
         if (a.Cash> b.Cash)
            return -1;
         return 0;
   });

只需选择所需对象的“Cash”属性并对其进行排序即可。 希望它有所帮助。