我试图对包含具有“在线”、“离线”、“忙碌”状态键的对象数组进行排序,因此只想以所有“在线”都出现在顶部的方式对数组进行排序,然后是“忙碌” ”然后“离线”
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);
Run Code Online (Sandbox Code Playgroud)
这只会返回我的状态:顶部的“在线”。谢谢