Skip to content

Instantly share code, notes, and snippets.

@juanbrusco
Created May 23, 2019 18:43
Show Gist options
  • Save juanbrusco/d2f225a466d99da4b5d59a9f1bb82dd3 to your computer and use it in GitHub Desktop.
Save juanbrusco/d2f225a466d99da4b5d59a9f1bb82dd3 to your computer and use it in GitHub Desktop.
Sort arrays by key- Javascript
//https://www.sitepoint.com/sort-an-array-of-objects-in-javascript/
compare(a, b) {
const genreA = a.servicebillingfact.rec_version_num;
const genreB = b.servicebillingfact.rec_version_num;
let comparison = 0;
if (genreA < genreB) {
comparison = 1;
} else if (genreA > genreB) {
comparison = -1;
}
return comparison;
}
compareValues(key, order) {
return function (a, b) {
if (!a.hasOwnProperty(key) ||
!b.hasOwnProperty(key)) {
return 0;
}
const varA = (typeof a[key] === 'string') ?
a[key].toUpperCase() : a[key];
const varB = (typeof b[key] === 'string') ?
b[key].toUpperCase() : b[key];
let comparison = 0;
if (varA > varB) {
comparison = 1;
} else if (varA < varB) {
comparison = -1;
}
return (
(order === 'desc') ?
(comparison * -1) : comparison
);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment