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

sorting objects by property #10

Open
nmokkenstorm opened this issue Apr 21, 2021 · 1 comment
Open

sorting objects by property #10

nmokkenstorm opened this issue Apr 21, 2021 · 1 comment

Comments

@nmokkenstorm
Copy link

Hey, thanks for creating this essential functionality for the JS/TS ecosystem. I have a feature request that I'd be willing to look into implementing as a PR, but I figured I'd raise an issue first to see if that's something that'd be appreciated.

I'd like to be able to sort an array of objects 'naturally' by some property:

const items = [
 {
   name: 'Foo',
   created: '04/21/2021'
  },
  {
   name: 'Bar',
   created: '04/20/2021'
  }
].sort(natsort({property: 'created'}))

Or even use an accessor function, which would be useful for nested items: [...].sort(natsort({property: (item) => item.nestedItem.created}))

I'd love to hear some thoughts.

@bubkoo
Copy link
Owner

bubkoo commented Sep 15, 2021

@nmokkenstorm

var sorter = natsort();
items.sort(function(a, b) {
  return sorter(a.created, b.created);
});

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

2 participants