0

I want to sort my list. I have this JSON,

{
    "ships": [{
        "Code": "s01",
        "Quantity": "10",
        "Desc": "Ship 1", 
        "Date": "Jul 01 2016"
    },{
        "Code": "s03",
        "Quantity": "4",
        "Desc": "Ship 2", 
        "Date": "Jul 03 2016"
    },{
        "Code": "s02",
        "Quantity": "2",
        "Desc": "Ship 4", 
        "Date": "Jul 02 2016"
    }]
}

How can I sort it by Date, by Code, or Alphabetically?

I can use jQuery. I tried this code,

var element = ...; 
element.Sort();

It is not working.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
JMA
  • 974
  • 3
  • 13
  • 41

5 Answers5

3

You just need to implement your own sort() logic. Try this:

data.ships.sort(function(a, b) {
    return a.Date < b.Date ? -1 : a.Date > b.Date ? 1 : 0;
});

Example fiddle

You can change the property of the object which is sorted if needed. You can even make it in to a function to keep the code DRY:

function sortByProperty(array, propName) {
    array.sort(function(a, b) {
        return a[propName] < b[propName] ? -1 : a[propName] > b[propName] ? 1 : 0;
    }); 
}

sortByProperty(data.ships, 'Code');
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • can you explain the function in sort parameter? – JMA Jan 25 '16 at 12:54
  • The `sort()` iterates through the array comparing two values at a time. You return either `-1`, `0` or `1` depending on the comparison of the values. See MDN for more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort – Rory McCrossan Jan 25 '16 at 12:55
0

try with function:

function SortByName(x,y) {
  return ((x.Name == y.Name) ? 0 : ((x.Name > y.Name) ? 1 : -1 ));
}

// Call Sort By Name
arr.sort(SortByName);
Sumanta736
  • 695
  • 3
  • 10
0

With Only Javascript:

function compare(a,b) {
  if (a.last_nom < b.last_nom)
    return -1;
  else if (a.last_nom > b.last_nom)
    return 1;
  else 
    return 0;
}

shipArr.ships.sort(compare);

refer: Sort array of objects by string property value in JavaScript

With UnderScoreJs

var sortedObjs = _.sortBy( shipArr.ships, 'Code'); //can be 'Date' or 'Desc'
Community
  • 1
  • 1
0
var elements={
"ships": [{
    "Code": "s01",
    "Quantity": "10",
    "Desc": "Ship 1", 
    "Date": "Jul 01 2016"
},{
    "Code": "s03",
    "Quantity": "4",
    "Desc": "Ship 2", 
    "Date": "Jul 03 2016"
},{
    "Code": "s02",
    "Quantity": "2",
    "Desc": "Ship 4", 
    "Date": "Jul 02 2016"
}]
};

 var arr=elements.ships;

Get JSON Data like above. And write sorting function with specific column name.

 var sortColumnName = "Code";
 function SortByCode(x,y) {
  return ((x[sortColumnName]  == y[sortColumnName]) ? 0 : ((x[sortColumnName]>    y[sortColumnName]) ? 1 : -1 ));
}
 arr.sort(SortByCode);

After sorting print the sorted JSON array.

 document.write("<br/><b>After Sorting </b> <br/>");   
for(var n=0;n<arr.length;n++){
  document.write(arr[n].Code + ' ' + arr[n].Quantity + ' ' + arr[n].Desc + '<br>');
}
Thangadurai
  • 524
  • 1
  • 9
  • 20
0

use Underscore! http://underscorejs.org/

var sortedElement = _.sortBy(element.ships, 'Code');
ram dvash
  • 190
  • 1
  • 11