JavaScript sort multi dimensional array on specific columnsJavaScript sort multi dimensional array on specific columnsJavaScript sort multi dimensional array on specific columnsJavaScript sort multi dimensional array on specific columns
<script>
const data = [{
years: 15,
name: 'David',
age: 28
}, {
years: 20,
name: 'Joe',
age: 23
}, {
years: 12,
name: 'Tracy',
age: 28
}, {
years: 18,
name: 'Joel',
age: 25
}, {
years: 19,
name: 'Michael',
age: 40
}, {
years: 11,
name: 'Arnold',
age: 35
}, {
years: 15,
name: 'Paul',
age: 24
}, ];
console.log('sorted by Age (desc), Name (asc), Years (desc):', multiSort(data, {
age: 'desc',
name: 'asc',
years: 'desc'
}));
/**
* Sorts an array of objects by column/property.
* @param {Array} array - The array of objects.
* @param {object} sortObject - The object that contains the sort order keys with directions (asc/desc). e.g. { age: 'desc', name: 'asc' }
* @returns {Array} The sorted array.
*/
function multiSort(array, sortObject = {}) {
const sortKeys = Object.keys(sortObject);
// Return array if no sort object is supplied.
if (!sortKeys.length) {
return array;
}
// Change the values of the sortObject keys to -1, 0, or 1.
for (let key in sortObject) {
sortObject[key] = sortObject[key] === 'desc' || sortObject[key] === -1 ? -1 : (sortObject[key] === 'skip' || sortObject[key] === 0 ? 0 : 1);
}
const keySort = (a, b, direction) => {
direction = direction !== null ? direction : 1;
if (a === b) { // If the values are the same, do not switch positions.
return 0;
}
// If b > a, multiply by -1 to get the reverse direction.
return a > b ? direction : -1 * direction;
};
return array.sort((a, b) => {
let sorted = 0;
let index = 0;
// Loop until sorted (-1 or 1) or until the sort keys have been processed.
while (sorted === 0 && index < sortKeys.length) {
const key = sortKeys[index];
if (key) {
const direction = sortObject[key];
sorted = keySort(a[key], b[key], direction);
index++;
}
}
return sorted;
});
}
</script>
Most Helpful This Week
jQuery example of scroll to top
How can I redirect the user from one page to another using jQuery or pure JavaScript?
JavaScript HTML5 Validation for name and email field
How to change text color on click using JavaScript?
How to change the height of Textarea on click?
Image Rollover Using onMouseOver and onMouseOut