21
with an Array like this:
users = [
{ id: 1, fname: 'Fred', lname: 'Flinstone', state: 'CA' },
{ id: 2, fname: 'George', lname: 'Winston', state: 'FL' },
{ id: 3, fname: 'Luke', lname: 'Skywalker', state: 'CA' }
]
and you want to sort by last name with coffeescript, you can do this:
users.sort (a,b) ->
return if a.lname.toUpperCase() >= b.lname.toUpperCase() then 1 else -1
I tried using a function like this:
sortBy = (field, reverse, primer) ->
key = (x) ->
return if primer? then primer x[field] else x[field]
return (a,b) ->
A = key a
B = key b
return (A < B ? -1 : (A > B ? 1 : 0)) * [1,-1][+!!reverse]
which was invoked like this:
users.sort sortBy "lname", false, (a) ->
return a.toUpperCase()
but that didn't sort the array properly.
Is there a way to sort by more than 1 field, i.e. sort first by State, and then by Last Name? I was hoping to improve upon the "sortBy" function above and add ability to sort by at least 2 fields.