Search⌘ K
AI Features

Exercise: Sorting a Collection

Explore how to sort a collection of models in Marionette.js by creating and applying comparator functions. This lesson helps you understand sorting logic within collections, enabling you to display data in a customized, organized manner by first and last name sorting.

Sorting a collection with a function

Let’s say we have the following collection:

Node.js
var contacts = new ContactManager.ContactCollection([
{
firstName: "Alice",
lastName: "Tampen"
},
{
firstName: "Bob",
lastName: "Brigham"
},
firstName: "Alice",
lastName: "Artsy"
},
{
firstName: "Alice",
lastName: "Arten"
},
{
firstName: "Charlie",
lastName: "Campbell"
},
{
firstName: "Alice",
lastName: "Smith"
},
]);

If you run the code, you’ll see the contacts in the following order:

    ...