我正在尝试按年龄对员工对象数组进行排序,我得到以下输出:
[object Object],[object Object],[object Object],[object Object]
但是,当我将员工数组替换为以下数字数组时,代码可以正常工作: var points=[2,10,7,8]
有人可以告诉我我要去哪里错了吗?谢谢你。
以下是我的代码:
<script type="text/javascript">
function sortFunc(){
var employees=[]
employees[0]={name:"George", age:32, retiredate:"March 12, 2014"}
employees[1]={name:"Edward", age:17, retiredate:"June 2, 2023"}
employees[2]={name:"Christine", age:58, retiredate:"December 20, 2036"}
employees[3]={name:"Sarah", age:62, retiredate:"April 30, 2020"}
employees.sort(function(a, b){
return a.age-b.age
// return a-b;
})
document.getElementById("disp").innerHTML=employees;
}
</script>
</head>
<body>
<div id="disp">Click the button below to sort.</div>
<button onclick="sortFunc()">Sort Now</button>
</body>