根据许多条件对数组中的项进行排序

4 javascript ecmascript-6

我有一系列正确的分数,如下所示:

var correctScores = [
   {score: "2-2", code: "draw"},
   {score: "2-1", code: "home"},
   {score: "0-1", code: "away"},
   {score: "1-0", code: "home"},
   {score: "3-0", code: "home"},
   {score: "any home win", code: "home"},
   {score: "1-2", code: "away"},
   {score: "0-3", code: "away"},
   {score: "any draw", code: "draw"},
   {score: "0-0", code: "draw"},
   {score: "any away win", code: "away"},
   {score: "0-2", code: "away"},
   {score: "1-1", code: "draw"},
   {score: "2-0", code: "home"}
]
Run Code Online (Sandbox Code Playgroud)

我已成功地对它们进行排序的home,draw,away,但我想这延伸到比分也让他们处于一致的顺序.当我说一致的顺序时,我想要对得分进行排序,以便最少数量的目标在阵列中排在第一位.如果目标数量相同,则应首先显示具有更多主目标的游戏.

目前我的代码看起来是这样的,只有通过种种home,draw,away.我发现很难扩展这种排序方法也按分数排序.分数也将不断更新,因此这些分数不一定总是与数组中的分数相同.

let order = { HOME: 1, DRAW: 2, AWAY: 3 };

correctScores.sort(function (a, b) { return order[a.code] - order[b.code]; });
Run Code Online (Sandbox Code Playgroud)

我试图让数组看起来像这样:

var correctScores = [
   {score: "1-0", code: "home"},
   {score: "2-0", code: "home"},
   {score: "3-0", code: "home"},
   {score: "2-1", code: "home"},
   {score: "any home win", code: "home"}

   {score: "0-0", code: "draw"},
   {score: "1-1", code: "draw"},
   {score: "2-2", code: "draw"},
   {score: "any draw", code: "draw"},

   {score: "0-1", code: "away"},
   {score: "0-2", code: "away"},
   {score: "0-3", code: "away"},
   {score: "1-2", code: "away"},
   {score: "any away win", code: "away"}

]
Run Code Online (Sandbox Code Playgroud)

任何人都可以给我任何关于如何扩展这种排序方法的建议吗?

Nin*_*olz 5

基本上,您需要订单对象的小键,您需要拆分分数以进行排序.

然后,您可以从索引1开始链接增量,并按索引零的增量排序.

var correctScores = [{ score: "2-2", code: "draw" }, { score: "2-1", code: "home" }, { score: "0-1", code: "away" }, { score: "1-0", code: "home" }, { score: "3-0", code: "home" }, { score: "1-2", code: "away" }, { score: "0-3", code: "away" }, { score: "0-0", code: "draw" }, { score: "0-2", code: "away" }, { score: "1-1", code: "draw" }, { score: "2-0", code: "home" }],
    order = { home: 1, draw: 2, away: 3 };

correctScores.sort(function (a, b) {
    function getParts(s) { return s.split('-'); }
    var aa = getParts(a.score),
        bb = getParts(b.score);

    return order[a.code] - order[b.code] || aa[1] - bb[1] || aa[0] - bb[0];
});

console.log(correctScores);
Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Run Code Online (Sandbox Code Playgroud)

更新问题的解决方案,检查分数是否不可拆分,然后将数组Infinite作为值,因为它将排序到底部.

var correctScores = [{ score: "2-2", code: "draw" }, { score: "2-1", code: "home" }, { score: "0-1", code: "away" }, { score: "1-0", code: "home" }, { score: "3-0", code: "home" }, { score: "any home win", code: "home" }, { score: "1-2", code: "away" }, { score: "0-3", code: "away" }, { score: "any draw", code: "draw" }, { score: "0-0", code: "draw" }, { score: "Any away win", code: "away" }, { score: "0-2", code: "away" }, { score: "1-1", code: "draw" }, { score: "2-0", code: "home" }],
    order = { home: 1, draw: 2, away: 3 };

correctScores.sort(function (a, b) {
    function getParts(s) {
        var t = s.split('-');
        return t.length === 1 ? [Infinity, Infinity] : t;
    }
    var aa = getParts(a.score),
        bb = getParts(b.score);

    return order[a.code] - order[b.code] || aa[1] - bb[1] || aa[0] - bb[0];
});

console.log(correctScores);
Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Run Code Online (Sandbox Code Playgroud)