Created
January 8, 2017 00:57
-
-
Save dengue8830/01f51904335396e2a37e79a6c504e538 to your computer and use it in GitHub Desktop.
Very simple backbone sorted collection by two fields. Simplified version of https://gist.github.com/nordyke/1524046#file-multisortcollection-js-L44
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Original code https://gist.github.com/nordyke/1524046#file-multisortcollection-js-L44 | |
function sortAthletes(athletes){ | |
//points is the first criteria | |
//we sort athletes by points and make a group for all wich have the same points | |
var sortedGroups = _(athletes.models).chain(). | |
sortBy(function (model) { | |
return -model.get('points'); | |
}). | |
groupBy(function (model) { | |
return -model.get('points'); | |
}). | |
toArray(). | |
value(); | |
//time is the second criteria | |
//now we sort the point's sorted groups by time | |
//those that have the same points (group) will be compared by their time | |
_(sortedGroups).each(function (modelSet, index) { | |
sortedGroups[index] = _(sortedGroups[index]).sortBy(function(athlete){ | |
return athlete.get('time'); | |
}); | |
}); | |
//we put the groups on a plain json version | |
return new BaseCollection(_(sortedGroups).flatten()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment