Last active
December 17, 2015 18:59
-
-
Save killtw/5657102 to your computer and use it in GitHub Desktop.
sort object by key
This file contains hidden or 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
ksort = (obj, method=true) -> | |
keys = for key of obj | |
key | |
if method then keys.sort() else keys.reverse() | |
new_obj = {} | |
new_obj[key] = obj[key] for key in keys | |
new_obj | |
ksort(obj) | |
ksort(obj, false) # reverse |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment