我正在尝试通过属性title
对对象数组进行排序。 这是我运行的代码片段,但它不会对任何内容进行排序。 数组按原样显示。 P.S 我查看了以前类似的问题。 例如,这个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>