Created
June 12, 2017 08:10
-
-
Save pste/8915a860e3aaf33e3fba1ab93f5de962 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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