Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you replace lodash to sort an array of objects by object key? #331

Open
gilles6 opened this issue Oct 20, 2021 · 2 comments
Open

Comments

@gilles6
Copy link

gilles6 commented Oct 20, 2021

How do you replace lodash to sort an array of objects by object ('foo') key ('bar')?

_.sortBy(myarray, 'foo.bar')
@unquietwiki
Copy link

@gilles6 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries

I've been using this plugin, and running into stuff like this; I may try coming up with some updates to submit based on my findings.

@briancodes
Copy link

briancodes commented Dec 24, 2022

This is probably one of the reasons the docs say (may not) need lodash

Likely a few steps needed here for this replacement

  • copy the array: spread or concat
  • properties by dot notation string: split, reduce (with null/object checks)
  • comparator function used with native sort

Note that lodash also allows multi field sorting, _.sortBy(users, ['user', 'age']), which would add to the complexity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants