0
var json = $.parseJSON(data);
var serializedMyObj = JSON.stringify(json);
console.log(serializedMyObj); 

I displayed the json object in console shown below . NOW i want to sort it by Price .

[{
    "name": "HTC 816",
    "Manufacture": "htc",
    "price": "45.00",
    "url": "images/products/htc-desire-816-400x400.jpeg",
    "productId": "543J423"
}, {
    "name": "HTC 616",
    "Manufacture": "htc",
    "price": "55.00",
    "url": "images/products/htc-desire-616.jpeg",
    "productId": "543J424"
}, {
    "name": "HTC 716",
    "Manufacture": "htc",
    "price": "32.00",
    "url": "img/list grid.png",
    "productId": "543J425"
}, {
    "name": "HTC 416",
    "Manufacture": "htc",
    "price": "70.00",
    "url": "img/list grid.png",
    "productId": "543J426"
}, {
    "name": "HTC 316",
    "Manufacture": "htc",
    "price": "99.00",
    "url": "",
    "productId": "543J427"
}, {
    "name": "HTC 216",
    "Manufacture": "htc",
    "price": "45.00",
    "url": "",
    "productId": "543J428"
}, {
    "name": "HTC ONE",
    "Manufacture": "htc",
    "price": "25.00",
    "url": "images/products/htc-one-v.jpeg",
    "productId": "543J429"
}, {
    "name": "HTC EYE",
    "Manufacture": "htc",
    "price": "60.00",
    "url": "",
    "productId": "543J430"
}, {
    "name": "HTC DESIRE",
    "Manufacture": "htc",
    "price": "102.00",
    "url": "",
    "productId": "543J431"
}]
0

2 Answers 2

3
json.sort(function(a, b) { return a.price - b.price; });//sort ascending
Sign up to request clarification or add additional context in comments.

1 Comment

@RGraham it does work. It accepts any positive and negative numbers so a simple subtraction could be done. Read the article in your own link.
2

Use Array.sort:

var arr = [{
    "name": "HTC 816",
    "Manufacture": "htc",
    "price": "45.00",
    "url": "images/products/htc-desire-816-400x400.jpeg",
    "productId": "543J423"
}, {
    "name": "HTC 616",
    "Manufacture": "htc",
    "price": "55.00",
    "url": "images/products/htc-desire-616.jpeg",
    "productId": "543J424"
}, {
    "name": "HTC 716",
    "Manufacture": "htc",
    "price": "32.00",
    "url": "img/list grid.png",
    "productId": "543J425"
}, {
    "name": "HTC 416",
    "Manufacture": "htc",
    "price": "70.00",
    "url": "img/list grid.png",
    "productId": "543J426"
}, {
    "name": "HTC 316",
    "Manufacture": "htc",
    "price": "99.00",
    "url": "",
    "productId": "543J427"
}, {
    "name": "HTC 216",
    "Manufacture": "htc",
    "price": "45.00",
    "url": "",
    "productId": "543J428"
}, {
    "name": "HTC ONE",
    "Manufacture": "htc",
    "price": "25.00",
    "url": "images/products/htc-one-v.jpeg",
    "productId": "543J429"
}, {
    "name": "HTC EYE",
    "Manufacture": "htc",
    "price": "60.00",
    "url": "",
    "productId": "543J430"
}, {
    "name": "HTC DESIRE",
    "Manufacture": "htc",
    "price": "102.00",
    "url": "",
    "productId": "543J431"
}];

arr.sort(function(a,b){return a.price - b.price});
console.log(arr);

JSFIDDLE

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.