Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example -
- // { category: 'Cloths', 'brand': 'Van H', collection: 'New' }.sortObject(['brand', 'category', 'collection']);
- Object.prototype.sortObject = function(sortingArr) {
- var targetObj = this;
- var result = {}
- if (!targetObj) {
- return result;
- }
- sortingArr = sortingArr || []
- var sortedKeys = Object.keys(targetObj).sort(function(a, b) {
- return sortingArr.indexOf(a) - sortingArr.indexOf(b);
- });
- sortedKeys.forEach(function(key) {
- result[key] = targetObj[key];
- })
- return result;
- };
Add Comment
Please, Sign In to add comment