const cars = [ { make: "Toyota", model: "Camry", year: 2015 }, { make: "Honda", model: "Accord", year: 2013 }, { make: "Ford", model: "Mustang", year: 2020 } ]; const sortedCars = _.sortBy(cars, "year"); console.log(sortedCars); // Output: [ { make: "Honda", model: "Accord", year: 2013 }, { make: "Toyota", model: "Camry", year: 2015 }, { make: "Ford", model: "Mustang", year: 2020 } ]
const animals = [ { name: "Dog", legs: 4 }, { name: "Spider", legs: 8 }, { name: "Ant", legs: 6 } ]; const sortedAnimals = _.sortBy(animals, "legs"); console.log(sortedAnimals); // Output: [ { name: "Dog", legs: 4 }, { name: "Ant", legs: 6 }, { name: "Spider", legs: 8 } ]In example 2, the _sortBy method is used to sort an array of animal objects by the legs property. The sorted array is stored in the sortedAnimals variable and printed to the console. Package library: Underscore.js.