b12-archive / natural-sort

Sorting with support for numbers, dates, unicode and more.
MIT License
11 stars 5 forks source link

Work with nested structures, like a list of objects #5

Closed Trevoke closed 8 years ago

Trevoke commented 8 years ago

I was happily surprised to see that this works with Facebook's immutable! (that should be in the README, it's an attractive quality).

I would love it if I could specify a particular field in a nested structure, say, a list of maps, and sorting the list of maps based on that field on the maps.

Trevoke commented 8 years ago

Ignore me, that actually works just fine if you use something like:

var n = require("natural-sort")
var i = require("immutable")

var a = i.fromJS([{a: '1', field: "1a", name:'Alphonse'}, 
                  {a: '5', field: "a", name: 'Bethany'}, 
                  {a: '5', field: "B", name: 'George'}, 
                  {a: '8', field: "1", name: 'Donald'}, 
                  {a: '9', field: "10", name: 'Clothilde'}
                 ])

b = a.sortBy(function(x) { return x.get('name'); }, n())    
tomek-he-him commented 8 years ago

:+1: Thanks for sharing your thoughs! @bck2207 perhaps an example like this is worth showing in the readme?