JavaScript: Sort the specified array of objects by title value
JavaScript Array: Exercise-25 with Solution
Sort Objects by Title
Write a JavaScript function to sort the following array of objects by title value.
Sample object:
var library = [ { author: 'Bill Gates', title: 'The Road Ahead', libraryID: 1254}, { author: 'Steve Jobs', title: 'Walter Isaacson', libraryID: 4264}, { author: 'Suzanne Collins', title: 'Mockingjay: The Final Book of The Hunger Games', libraryID: 3245} ];
Visual Presentation:
Sample Solution:
JavaScript Code:
// Array of book objects in the library
var library = [
{ author: 'Bill Gates', title: 'The Road Ahead', libraryID: 1254},
{ author: 'Steve Jobs', title: 'Walter Isaacson', libraryID: 4264},
{ author: 'Suzanne Collins', title: 'Mockingjay: The Final Book of The Hunger Games', libraryID: 3245}
];
// Function to compare two book objects based on their titles for sorting
function compare_to_sort(x, y) {
// Check if the title of book x is less than the title of book y
if (x.title < y.title)
return -1;
// Check if the title of book x is greater than the title of book y
if (x.title > y.title)
return 1;
// If titles are equal, return 0
return 0;
}
// Output the sorted library array based on the compare_to_sort function
console.log(library.sort(compare_to_sort));
Output:
[{"author":"Suzanne Collins","title":"Mockingjay: The Final Book of The Hunger Games","libraryID":3245},{"author":"Bill Gates","title":"The Road Ahead","libraryID":1254},{"author":"Steve Jobs","title":"Walter Isaacson","libraryID":4264}]
Flowchart:
ES6 Version:
// Array of book objects in the library
const library = [
{ author: 'Bill Gates', title: 'The Road Ahead', libraryID: 1254},
{ author: 'Steve Jobs', title: 'Walter Isaacson', libraryID: 4264},
{ author: 'Suzanne Collins', title: 'Mockingjay: The Final Book of The Hunger Games', libraryID: 3245}
];
// Function to compare two book objects based on their titles for sorting
const compare_to_sort = (x, y) => {
// Check if the title of book x is less than the title of book y
if (x.title < y.title)
return -1;
// Check if the title of book x is greater than the title of book y
if (x.title > y.title)
return 1;
// If titles are equal, return 0
return 0;
};
// Output the sorted library array based on the compare_to_sort function
console.log(library.sort(compare_to_sort));
Live Demo:
See the Pen JavaScript - Sort the specified array of objects by title value - array-ex- 25 by w3resource (@w3resource) on CodePen.
Improve this sample solution and post your code through Disqus.
Previous: Write a JavaScript function to remove. 'null', '0', '""', 'false', 'undefined' and 'NaN' values from an array.
Next: Write a JavaScript program to find a pair of elements (indices of the two numbers) from an given array whose sum equals a specific target number.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics