0
  • My requirement is sorting objects based on the price.Based on the cash, objects should be sorted.
  • Below is the sample data which i consoled and got and showing below.

  • By means of consoling full_sky.sort(test); i am getting below data.


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

Output should be something like this

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 Answers3

0

For these kind of operations u can try using the lo-dash library. (http://lodash.com/docs)

Please check the docs for more. SortBy function should solve your problem Lo-Dash SortBy

0

Javascript doesn't ensure an order in objects and therefore sorting objects directly is not possible here. Please refer to this answer for better understanding on why and the relevant documentation. In your case, I would recreate a key-value pair list with the key as the cash attribute of your object and then sort that array directly modifying the sort function.

Assuming your initial array of objects is called list1, you could do:

 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;
 });

Here, I have overwritten the sort function a little bit to take care of the order. A relevant SO question on how the sort function works in javascript might be of further help.

Community
  • 1
  • 1
Vivek Pradhan
  • 4,777
  • 3
  • 26
  • 46
0

This can also be done like:

   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;
   });

Just have to pick the "Cash" property of the required object and sort it. Hope it helps.

Satya Ranjan Sahoo
  • 1,086
  • 7
  • 24