Skip to content

Instantly share code, notes, and snippets.

@auxcoder
Last active August 30, 2016 16:39
Show Gist options
  • Save auxcoder/a075e601a568dc91f19d82bc37568058 to your computer and use it in GitHub Desktop.
Save auxcoder/a075e601a568dc91f19d82bc37568058 to your computer and use it in GitHub Desktop.
Order array of objects by object.attribute (sort)
// function passed into sort to order by obj.key
function compare(a,b) {
if (a.sequence < b.sequence){
return -1;
}
if (a.sequence > b.sequence){
return 1;
}
return 0;
}
var array = [
{
sequence: 3,
title: 'Lorem Ipsum',
value: 125
},
{
sequence: 0,
title: 'Dolor at met',
value: 25
},
{
sequence: 1,
title: 'Quantum Mobile',
value: 15
}
];
console.log(array.sort(compare));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment