belmer
8/27/2016 - 2:23 PM

Sort Array Object

var arrayObj=[
{name:'Aaron',age:20},
{name:'Michael',age:15},
{name:'Meriam',age:18}
]

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

alert (JSON.stringify(arrayObj.sort(compare)))