Steven's Blog

A Dream Land of Peace!

JS中根据某个key来给object排序

I have something like the following,

1
2
3
var sophos = {name: "aa", time:"2016/03/02 01:12"};
var mcafee = {name: "bb", time:"2016/03/03 01:12"};
var trend = {name: "cc", time:"2016/03/04 01:12"};

and I want to find out the one with the earliest time, in the above code, it is “2016/03/02 01:12”, and the corresponding item is “sophos”, here is the realization is js code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
var sophos = {name: "aa", time:"2016/03/02 01:12"};
var mcafee = {name: "bb", time:"2016/03/03 01:12"};
var trend = {name: "cc", time:"2016/03/04 01:12"};

var obj = [sophos, mcafee, trend];

function compare(a, b){
    if (a.time < b.time){
        return -1;
    }
    else if (a.time > b.time){
        return 1;
    }
    else {
        return 0;
    }
}

var result = obj.sort(compare);
console.log(result);