
harwind
JavaScript Array of Objects Sorting: How to Sort by a Specific Object Property
I have an array of objects in JavaScript, and I want to sort them based on a specific property of the objects. For example, I have an array of books, and each book object has properties like ‘title’, ‘author’, and ‘publicationYear’. How can I sort this array of book objects alphabetically by the ‘title’ property?
Here’s a simplified version of the array:
const books = [
{ title: 'The Great Gatsby', author: 'F. Scott Fitzgerald', publicationYear: 1925 },
{ title: 'To Kill a Mockingbird', author: 'Harper Lee', publicationYear: 1960 },
{ title: '1984', author: 'George Orwell', publicationYear: 1949 }
];
This array should be sorted ascendingly using the ‘title’ parameter. I attempted to discover the answer by going to several sites like Scaler, but I was unable to locate the solution. Could you supply a JavaScript code sample that explains how to do this sorting and explains any key ideas or functions utilized in the code? I appreciate your help.
Most Liked

gulshan212
Well, here is a simple example of sorting.
const books = [
{ title: 'The Great Gatsby', author: 'F. Scott Fitzgerald', publicationYear: 1925 },
{ title: 'To Kill a Mockingbird', author: 'Harper Lee', publicationYear: 1960 },
{ title: '1984', author: 'George Orwell', publicationYear: 1949 }
];
// Sort the books array based on the 'title' property
books.sort((a, b) => {
const titleA = a.title.toLowerCase(); // Convert titles to lowercase for case-insensitive sorting
const titleB = b.title.toLowerCase();
if (titleA < titleB) {
return -1; // 'a' should come before 'b'
} else if (titleA > titleB) {
return 1; // 'b' should come before 'a'
} else {
return 0; // Titles are equal, no change in order
}
});
// Now, the books array is sorted alphabetically by title
console.log(books);
Thanks

kokolegorille
A one liner…
books.sort((a, b) => a.title.toLowerCase() < b.title.toLowerCase() ? -1 : 1)
Popular General Dev topics










Other popular topics










Categories:
Sub Categories:
- All
- In The News (10002)
- Dev Chat (200)
- Questions
- Resources (118)
- Blogs/Talks (26)
- Jobs (3)
- Events (14)
- Code Editors (58)
- Hardware (57)
- Reviews (4)
- Sales (15)
- Design & UX (4)
- Marketing & SEO (1)
- Industry & Culture (14)
- Ethics & Privacy (19)
- Business (4)
- Learning Methods (4)
- Content Creators (7)
- DevOps & Hosting (9)
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /ash
- /vscode
- /opensuse
- /centos
- /php
- /deepseek
- /html
- /zig
- /scala
- /debian
- /nixos
- /lisp
- /agda
- /react-native
- /textmate
- /sublime-text
- /kubuntu
- /arch-linux
- /revery
- /ubuntu
- /manjaro
- /django
- /spring
- /diversity
- /nodejs
- /lua
- /c
- /julia
- /slackware
- /neovim