2

I have got three similar json objects. Now i wish to join the entire list and then sort the complete list according to one of the index. Here, goes the objects description

Object 1

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

Object 2

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

Object 3

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

Once, I join all of them , I wish to sort the complete array (new one) w.r.t to price index.

Any hint for this will be appreciated. thanks :)

Prashant Singh
  • 3,725
  • 12
  • 62
  • 106
  • The three objects will not be having same data – Prashant Singh Jan 15 '12 at 10:50
  • 1
    Where are you stuck? [How to parse JSON](http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript), [how to concatenate arrays](http://stackoverflow.com/questions/5080028/what-is-the-most-efficient-way-to-concatenate-n-arrays-in-javascript), [how to sort an array of objects](http://stackoverflow.com/questions/1129216/sorting-objects-in-an-array-by-a-field-value-in-javascript/1129270#1129270) or something else? – Felix Kling Jan 15 '12 at 10:53
  • I am not able to concate them – Prashant Singh Jan 15 '12 at 10:53
  • Then follow the link in my comment. FYI, if you have already parsed the JSON into JavaScript objects/arrays, then your question is not related to JSON anymore. – Felix Kling Jan 15 '12 at 10:54
  • What is more preferrable, to concat JSON object or to javascript object ? – Prashant Singh Jan 15 '12 at 11:10
  • I'd say working with the objects/arrays in JavaScript is easier and less error-prone than string processing – Felix Kling Jan 15 '12 at 11:13

3 Answers3

2

if Object1 , Object2 and Object3 are JSON strings convert it to Javascript objects using eval function.

Then merge them using concat method. http://www.w3schools.com/jsref/jsref_concat_array.asp

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

Then sort using sort method in Javascript Array. 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;



});

Use return priceB - priceA; for descending order. jsfiddle : http://jsfiddle.net/diode/FzzHz/

.

Diode
  • 24,570
  • 8
  • 40
  • 51
1

Convert them to object and this should do the trick

Community
  • 1
  • 1
ladar
  • 5,858
  • 3
  • 26
  • 38
1

You could use the concat method to concatenate the arrays:

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

and then you could sort the resulting array.

Let's write the sort function that will sort them using the prod property (you could sort them by any property you wish):

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

and then sort:

result.sort(SortByProd);
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928