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
2
  • what is test in full_sky.sort(test) Commented Oct 6, 2014 at 6:46
  • response from api getting in the form of json , assigning the data to variables and pushing into full_sky.push(test); Commented Oct 6, 2014 at 6:49

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.