You-Dont-Need-Lodash-Underscore icon indicating copy to clipboard operation
You-Dont-Need-Lodash-Underscore copied to clipboard

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

Open gilles6 opened this issue 4 years ago • 2 comments

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

_.sortBy(myarray, 'foo.bar')

gilles6 avatar Oct 20 '21 09:10 gilles6

@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.

unquietwiki avatar Mar 01 '22 22:03 unquietwiki

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

briancodes avatar Dec 24 '22 20:12 briancodes