我想通过属性title
对对象数组排序。这是我正在运行的代码片段,但它不排序任何东西。该数组按原样显示。我看过以前的类似问题。这个例如here建议并使用我正在使用的相同方法。按字符串属性对象排序对象的数组
的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();
的HTML代码:
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h1> Test Page </h1>
<script src="myscript.js"> </script>
</body>
</html>
“比尔盖茨” - “史蒂夫乔布斯”应该是什么?无限或更不是数字;)? –