The following function will allow you to sort an array of objects on one or multiple properties, either ascending (default) or descending on each property and allow you to choose whether or not to perform case sensitive comparisons. By default, this function performs case insensitive sorts.
There are many code snippets available online or on many other blogs and websites but everyone is not able to optimize your blog or website so you need some optimized code snippet. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready to use code and paste it where you want.
Table of Contents
Features:
- Light Weight.
- Pure Vanilla JavaScript.
- Cross Browser.
- No External Files.
- Fully Customizable.
- Sort As Per ASC/DESC.
- Sort wrt Multiple Column.
How To Sort An Array With Respect To Multiple Column In ASC/DESC Order Using JavaScript?
There are a few easy and understandable steps to achieve your desired functionality using pure Vanilla JavaScript that we are gonna share below. Follow each step perfectly.
JavaScript:
/** * Index of Multidimensional Array * @param arr {!Array} - the input array * @param keys {Object} - object of sorting order and coloum name * @return {Array} */ function sortBy (arr, keys) { return arr.sort(function sort (i1,i2, sKeys=keys) { // Get order and key based on structure const compareKey = (sKeys[0].key) ? sKeys[0].key : sKeys[0]; const order = sKeys[0].order || 'ASC'; // ASC || DESC // Calculate compare value and modify based on order let compareValue = i1[compareKey].toString().localeCompare(i2[compareKey].toString()) compareValue = (order.toUpperCase() === 'DESC') ? compareValue * -1 : compareValue // See if the next key needs to be considered const checkNextKey = compareValue === 0 && sKeys.length !== 1 // Return compare value return (checkNextKey) ? sort(i1, i2, sKeys.slice(1)): compareValue; }) }
How To Use?:
You can use it on associative (key/value) arrays as well as multidimensional arrays also. Just call the below code.sortBy(your_array, [{key:"lastKeyName",order:'desc'}, {key:"secondKeyName",order:'desc'}, {key:"firstKeyName",order:'desc'}]);
You can write index number also instead of the key name. It will sort the last key first then will go step by step respectively.
Customization:
No need to customize it. Just copy-paste. Rest edit the code as per comments and need.
Troubleshooting the Errors:
Do it with concentration and patience. Check your alls steps and again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.
Final Words:
That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts and problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.
Be the first to write a comment.