Sorting an array of objects by property value

by Ronny

JavaScript

var a = [{name: 'a', order: 42}, {name: 'b', order: 123}, {name: 'c', order: 254}];

console.log('before:', a);
a.sort(function(a, b){
    return a.order - b.order;
});
console.log('after:', a);

var s = '';
$.each(a, function(i, el){
    s += JSON.stringify(a[i]) + ', ';
});
s = s.replace(/,\s$/, '');

console.log(s);