2017-08-28 3 views
0

Ich versuche, ein Array von Objekt durch eine Eigenschaft title zu sortieren. Dies ist das Code-Snippet, das ich leite, aber es sortiert nichts. Das Array wird so angezeigt, wie es ist. P. Ich habe mir vorher ähnliche Fragen angeschaut. Dieses zum Beispiel here schlägt vor und verwendet die gleiche Methode, die ich verwende.Javascript Sortierung Array von Objekten durch String-Eigenschaft

Die 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(); 

Der HTML-Code:

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

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

</html> 
+0

"Bill Gates" - "Steve Jobs" sollte was sein? Unendlichkeit oder eher keine Zahl;)? –

Antwort

1

Haben Sie so versucht? Es arbeitet als

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);

Ref erwartet: Sort array of objects by string property value in JavaScript

0

Subtraction ist für numerische Operationen. Verwenden Sie stattdessen a.title.localeCompare(b.title).

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();

1

Verwenden Sie den < oder> Operator, wenn Strings im Vergleichsfunktion verglichen wird.

see documentation

+1

Verwenden Sie den Operator> für diese Instanz, dies funktioniert, wenn Sie den Minus-Operator durch> ersetzen. –

+1

@ jonathan.ihm: Der Callback '.sort()' erwartet ein numerisches Ergebnis, keinen booleschen, so dass mehr als nur ein Ersatz benötigt wird. – spanky

+0

Ich lief es in der Konsole und bestätigte, dass es sofort funktionierte. Siehe auch https://stackoverflow.com/questions/51165/how-to-sort-strings-in-javascript –

-1

können Sie versuchen, diese

FÜR DESC

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

oder FÜR ASC

library.sort(function(a,b){return a.title > b.title;}); 
Verwandte Themen