Last active
October 22, 2021 15:16
-
-
Save colingourlay/82506396503c05e2bb94 to your computer and use it in GitHub Desktop.
Lodash / Underscore sort object keys. Like _.sortBy(), but on keys instead of values, returning an object, not an array. Defaults to alphanumeric sort.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var obj = {b: 3, c: 2, a: 1}; | |
_.sortKeysBy(obj); | |
// {a: 1, b: 3, c: 2} | |
_.sortKeysBy(obj, function (value, key) { | |
return value; | |
}); | |
// {a: 1, c: 2, b: 3} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_.mixin({ | |
'sortKeysBy': function (obj, comparator) { | |
var keys = _.sortBy(_.keys(obj), function (key) { | |
return comparator ? comparator(obj[key], key) : key; | |
}); | |
return _.object(keys, _.map(keys, function (key) { | |
return obj[key]; | |
})); | |
} | |
}); |
There is no _.object
in Lodash, should be _.zipObject
v0.4.0: Added _.zipObject
v0.7.0: Renamed _.zipObject
to _.object
v1.1.0: Made _.object
an alias of _.zipObject
- Removed
_.object
in favor of_.fromPairs
- Split
_.zipObject
into_.fromPairs
In other words, _.object
hasn't been available since v4.0.0 (Jan. 12, 2016), _.zipObject
has been available since v1.1.0 (Mar. 26, 2013).
when using fromPairs
it would be:
const sortByKeys = object => {
const keys = Object.keys(object)
const sortedKeys = _.sortBy(keys)
return _.fromPairs(
_.map(sortedKeys, key => [key, object[key]])
)
}
In case anyone requires it.
This is not a mixin, but you can make one if needed, we use it as a util though.
Thank you so much!
You are awesome..
@gor181 Gotcha !
@gor181 Thank you so much ! that's awesome !
@gor181 Great!! @ @
Thank you!!
@gor181 - thanks!
Hi! I'm having trouble reversing this object using the function tho
const object = {
202: {
id: 61,
play_date: '2020-05-28 16:53:00',
round: 204,
status: 'N'
},
203: {
id: 66,
play_date: '2020-05-28 16:53:00',
round: 203,
status: 'N'
}
}
Expected output will be:
{
203: {
id: 66,
play_date: '2020-05-28 16:53:00',
round: 203,
status: 'N'
},
202: {
id: 61,
play_date: '2020-05-28 16:53:00',
round: 204,
status: 'N'
}
}
Small variation that works with nested objects as well:
const sortByKeys = object => {
const keys = Object.keys(object);
const sortedKeys = _.sortBy(keys);
return _.fromPairs(
_.map(sortedKeys, key => {
let value = object[key];
if (typeof object[key] === "object" && !(object[key] instanceof Array)) {
value = sortByKeys(value);
}
return [key, value];
})
);
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice job! I found this when writing my own version, which did the exact same thing, but no comparator. So I used yours, http://pastebin.com/XWv803UF