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 :)

6
  • The three objects will not be having same data Commented Jan 15, 2012 at 10:50
  • 1
    Where are you stuck? How to parse JSON, how to concatenate arrays, how to sort an array of objects or something else? Commented Jan 15, 2012 at 10:53
  • I am not able to concate them Commented Jan 15, 2012 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. Commented Jan 15, 2012 at 10:54
  • What is more preferrable, to concat JSON object or to javascript object ? Commented Jan 15, 2012 at 11:10

3 Answers 3

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/

.

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

2 Comments

can you please explain the last part? After merging array, I am not getting how this code sorts them. Please explain a bit
Thanks a lot. I got everything :D
1

Convert them to object and this should do the trick

Comments

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

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.