13

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>
user8528721
  • 133
  • 1
  • 1
  • 4

5 Answers5

16

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);
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • Is there any way to order this non-alphabetically, but based on another pre-set array? For example, order an entire list of names and functions but in the order of a pre-set array of functions ( chef above sous-chef, above chef de parties, ... ). – nclsvh Mar 21 '22 at 07:46
3

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();
spanky
  • 2,768
  • 8
  • 9
2

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

see documentation

big nol
  • 41
  • 1
  • 2
  • 1
    Use the > operator for this instance, this works when replacing the minus operator with >. – jonathan.ihm Aug 28 '17 at 18:04
  • 1
    @jonathan.ihm: The `.sort()` callback expects a numeric result, not a boolean, so more than just a drop-in replacement would be needed. – spanky Aug 28 '17 at 18:11
  • I ran it in console and confirmed it worked right away. See also https://stackoverflow.com/questions/51165/how-to-sort-strings-in-javascript – jonathan.ihm Aug 28 '17 at 18:14
  • Nevermind, see https://stackoverflow.com/questions/34466125/sort-strings-by-using-less-greater-than-comparison-operators-in-comparator as well I stand corrected. – jonathan.ihm Aug 28 '17 at 18:20
0

you can try this code from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

library.sort(function(a, b){
var tA = a.title.toUpperCase();
  var tB = b.title.toUpperCase();
  if (tA < tB) {
    return -1;
  }
  if (tA > tB) {
    return 1;
  }
  return 0;
})
Christian
  • 151
  • 1
  • 11
-1

Can you try this

FOR DESC

library.sort(function(a,b){return a.title < b.title;});

or FOR ASC

library.sort(function(a,b){return a.title > b.title;});
Nuttertools
  • 120
  • 1
  • 6