Back To Blog Posts
Sorting Backbone Collections By Attributes Other Than Model ID
By default, Backbone.Collection
is sorted by model id (usually from the database). If you want to sort by another attribute, you need to define a comparator
function. Here is an example that words for a boolean
value
After adding code like this to your collection, you can simply make a simple call like
And if attribute
is boolean, the trues fill float to the top of the collection. Also, note that youc an re-render elements in your UI by listening to the sort
call: