Skip to content

Instantly share code, notes, and snippets.

@jfolds
Last active April 15, 2019 19:36
Show Gist options
  • Save jfolds/de17d5b49fbade942d54ee306701257b to your computer and use it in GitHub Desktop.
Save jfolds/de17d5b49fbade942d54ee306701257b to your computer and use it in GitHub Desktop.
function sortData(data, column, direction) {
const sortedData = data.sort((a, b) => {
let val1 = a[column];
let val2 = b[column];
val1 = typeof val1 === 'string' ? val1.toUpperCase() : val1;
val2 = typeof val2 === 'string' ? val2.toUpperCase() : val2;
return (val1 < val2)
? -1
: 1
? (val1 > val2)
: 1
? -1
: 0;
});
if (direction === 'desc') {
sortedData.reverse();
}
return sortedData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment