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>
"Bill Gates" - "Steve Jobs" sollte was sein? Unendlichkeit oder eher keine Zahl;)? –