Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript sorting array of objects by string property [duplicate]

Tags:

javascript

I am trying to sort an array of object by a property title. This the code snippet that I am running but it does not sort anything. The array is displayed as it is. P.S I looked at previous similar questions. This one for example here suggests and uses the same method I am using.

The javascript:

function sortLibrary() {
    // var library is defined, use it in your code
    // use console.log(library) to output the sorted library data
    console.log("inside sort");
    library.sort(function(a,b){return a.title - b.title;});
    console.log(library);
} 

// tail starts here
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
    }
];

sortLibrary();

The html code:

<html>
<head>
    <meta charset="UTF-8">
</head>

<body>
<h1> Test Page </h1>
<script src="myscript.js"> </script>
</body>

</html>
like image 828
user8528721 Avatar asked Aug 28 '17 17:08

user8528721


People also ask

How do you sort an array of objects by a string property value in JavaScript?

Sort an Array of Objects in JavaScript To sort an array of objects, you use the sort() method and provide a comparison function that determines the order of objects.


3 Answers

Have you tried like this? It is working as expected

library.sort(function(a,b) {return (a.title > b.title) ? 1 : ((b.title > a.title) ? -1 : 0);} );

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
    }
];
console.log('before sorting...');
console.log(library);
library.sort(function(a,b) {return (a.title > b.title) ? 1 : ((b.title > a.title) ? -1 : 0);} );

console.log('after sorting...');
console.log(library);
like image 176
Always Sunny Avatar answered Oct 17 '22 00:10

Always Sunny


Subtraction is for numeric operations. Use a.title.localeCompare(b.title) instead.

function sortLibrary() {
  console.log("inside sort");
  library.sort(function(a, b) {
    return a.title.localeCompare(b.title);
  });
  console.log(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
  }
];

sortLibrary();
like image 3
spanky Avatar answered Oct 17 '22 02:10

spanky


Use the < or > operator when comparing strings in your compare function.

see documentation

like image 2
big nol Avatar answered Oct 17 '22 00:10

big nol