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"
}]
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176

2 Answers2

3
json.sort(function(a, b) { return a.price - b.price; });//sort ascending
Dmitry
  • 6,716
  • 14
  • 37
  • 39
  • @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. – Dmitry Oct 26 '14 at 07:09
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

Amir Popovich
  • 29,350
  • 9
  • 53
  • 99