【发布时间】:2017-09-28 10:09:59
【问题描述】:
我试图对包含状态键的对象数组进行排序,其中状态键具有 "online","offline","Busy" ,所以只是想以这样一种方式对数组进行排序,使所有“online”都出现在顶部,然后按“忙”然后“离线”
var arr = [{_id: "58e21249", name: "test2", status: "offline"},
{_id: "58e1249", name: "test3", status: "online"},
{_id: "58qwe49", name: "test21", status: "offline"},
{_id: "58ed49", name: "test212", status: "online"},
{_id: "58ee49", name: "test23", status: "offline"},
{_id: "58xe49", name: "test12", status: "online"},
{_id: "5849", name: "test2323", status: "busy"},
{_id: "58er49", name: "test2121", status: "busy"}];
arr.sort(function(first, second) {
if (second.status == "online") return 1;
});
console.log(arr);
这只会返回我的状态:顶部的“在线”。谢谢
【问题讨论】:
-
注意:meta 上提到了这个问题。
标签: javascript arrays sorting