按多个值

时间:2016-08-24 08:38:34

标签: javascript arrays sorting

这是我的对象数组

[{"name":"Tania","country":"BR"},{"name":"Carl","country":"BR"},{"name":"Farl","country":"BR"},{"name":"Rany","country":"BR"},{"name":"Ron","country":"JP"},{"name":"Antony","country":"JP"}]

我想按名称和同一国家对此数组进行排序。 这是结果

[{"name":"Carl","country":"BR"},{"name":"Farl","country":"BR"},{"name":"Rany","country":"BR"},{"name":"Tania","country":"BR"},{"name":"Antony","country":"JP"},{"name":"Ron","country":"JP"}]

要仅对我制作的名称进行排序,但在这种情况下不按国家/地区划分,我该怎么做?

array.sort((f , n)=> {
        var nameF = f.name.toUpperCase();
        var nameN = n.name.toUpperCase();

        if(nameF < nameN) return -1;
        else if(nameF > nameN) return 1;
        else return 0
      });

0 个答案:

没有答案