Skip to content

Instantly share code, notes, and snippets.

@vijaydeepak-tt
Created September 25, 2019 11:16
Show Gist options
  • Save vijaydeepak-tt/824d65caf90a5dc3231f2322d60c5b09 to your computer and use it in GitHub Desktop.
Save vijaydeepak-tt/824d65caf90a5dc3231f2322d60c5b09 to your computer and use it in GitHub Desktop.
sortArrObjects(
key: string,
order: string = 'asc',
isDate: boolean = false
) {
return (a, b) => {
if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
// if property doesn't exist on either object
return 0;
}
let varA;
let varB;
if (isDate) {
varA = new Date(a[key]);
varB = new Date(b[key]);
} else {
varA = typeof a[key] === 'string' ? a[key].toUpperCase() : a[key];
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 === 'asc' ? comparison * -1 : comparison;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment