我有一系列正确的分数,如下所示:
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"}
]
我已设法按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]; });
我试图让数组看起来像这样:
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"}
]
任何人都可以就如何扩展这种排序方法给我任何建议吗?
答案 0 :(得分: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);

.as-console-wrapper { max-height: 100% !important; top: 0; }

更新问题的解决方案,检查分数是否不可拆分,然后选择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);

.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)
那样
var correctScores = [
{score: "2-1", code: "home"},
{score: "1-0", code: "home"},
{score: "3-0", code: "home"},
{score: "2-0", code: "home"},
{score: "1-1", code: "draw"},
{score: "0-0", code: "draw"},
{score: "2-2", code: "draw"},
{score: "0-3", code: "away"},
{score: "0-2", code: "away"},
{score: "0-1", code: "away"},
{score: "1-2", code: "away"}
];
var groups = {};
var scores = [];
correctScores.map(function(e) {
groups[e.code] = groups[e.code] || [];
groups[e.code].push(e);
});
for(var i in groups) {
groups[i].sort(function (a, b) {
var scoreA = a.score.split('-').map(Number);
var scoreB = b.score.split('-').map(Number);
return scoreA[0]+scoreA[1] > scoreB[0]+scoreB[1] ? 1:(
scoreA[0]+scoreA[1] < scoreB[0]+scoreB[1] ? -1:0);
});
scores = scores.concat(groups[i]);
}
console.log(scores);
答案 2 :(得分:0)
包括新要求:
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"}
];
let order = { home: 1, draw: 2, away: 3 };
correctScores.sort(function (a, b) {
s1 = parseInt(a.score.split("-")[0]);
s2 = parseInt(b.score.split("-")[0]);
s1s = s1+parseInt(a.score.split("-")[1]);
s2s = s2+parseInt(b.score.split("-")[1]);
if(a.score.toLowerCase().indexOf("any")>-1){s1=999;s1s=999;}
if(b.score.toLowerCase().indexOf("any")>-1){s2=999;s2s=999;}
if(order[a.code]<order[b.code]) return -1;
if(order[a.code]>order[b.code]) return 1;
if(s1s<s2s) return -1;
if(s1s>s2s) return 1;
if(s1>s2) return -1;
if(s1<s2) return 1;
});
console.log(correctScores);