Skip to content

Instantly share code, notes, and snippets.

@pste
Created June 12, 2017 08:10
Show Gist options
  • Save pste/8915a860e3aaf33e3fba1ab93f5de962 to your computer and use it in GitHub Desktop.
Save pste/8915a860e3aaf33e3fba1ab93f5de962 to your computer and use it in GitHub Desktop.
var items = [{name:"a", age:22}, { name:"b", age:11}, {name:"a", age: 10}]
var sortingfields = {name:true, age:true} // This is used as a dictionary. Sort by "name" DESC and "age" ASC
// do the items sort
items.sort((a,b) => {
for (col in sortingfields) { // added properties are positional
if (a[col] != b[col]) { // if they are equal, sorting is made on the next column
if (sortingfields[col] === true) return a[col] > b[col] // if ASC === true
else return a[col] < b[col]
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment