【发布时间】:2018-02-06 01:36:35
【问题描述】:
我正在尝试按属性title 对对象数组进行排序。这是我正在运行的代码 sn-p 但它没有对任何东西进行排序。数组按原样显示。 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>
【问题讨论】:
-
“比尔盖茨” - “史蒂夫乔布斯”应该是什么?无穷大或更确切地说不是数字;)?
标签: javascript