javascript数组排序比较函数问题,我创建了下面的数组需要将一列排序为下降并保持一列作为上升,使用简单比较功能,但结果不合适。它很大程度上可以帮助我这个问题,
var homes =[
{score: "1.40", tier: "Tier-1", regionKey: 12, regionName: "Northern America"},
{score: "1.40", tier: "Tier-1", regionKey: 21, regionName: "Northern Europe"},
{score: "1.40", tier: "Tier-1", regionKey: 0, regionName: "Rest of World (ISO)"},
{score: "1.90", tier: "Tier-2", regionKey: 0, regionName: "Rest of World (ISO)"},
{score: "2.40", tier: "Tier-2", regionKey: 12, regionName: "Northern America"},
{score: "2.20", tier: "Tier-2", regionKey: 22, regionName: "Southern Europe"},
{score: "2.30", tier: "Tier-2", regionKey: 20, regionName: "Eastern Europe"},
{score: "1.80", tier: "Tier-2", regionKey: 10, regionName: "Central America"},
{score: "2.20", tier: "Tier-2", regionKey: 20, regionName: "Eastern Europe"},
{score: "1.80", tier: "Tier-2", regionKey: 22, regionName: "Southern Europe"},
{score: "2.60", tier: "Tier-3", regionKey: 65, regionName: "Eastern Europe"},
];
homes.sort(scoreComparison);
function scoreComparison(a, b) {
var tierA = a.tier;
var tierB = b.tier;
var scoreA = a.score*100;
var scoreB = b.score*100;
var comparison = 0;
if (tierA === tierB) {
if (scoreA > scoreB) {
comparison = -1;
} else if (scoreA < scoreB) {
comparison = 1;
}
return comparison;
}
}
console.log(homes);
用;
替换房屋声明下方的所有内容
homes.sort(
function(a, b) {
return a.score > b.score ? 1 : a.score < b.score ? -1 : 0;
}
).sort(
function(a, b){
return a.tier < b.tier ? -1 : a.tier > b.tier ? 1 : 0;
}
);
翻转&lt;>因此,取决于您要上升还是降序。
您可以通过获取列值的差来使用链接方法,如果上一个值为零,则可以获取下一个delty。
var homes = [{ score: "1.40", tier: "Tier-10", regionKey: 12, regionName: "Northern America" }, { score: "1.40", tier: "Tier-1", regionKey: 21, regionName: "Northern Europe" }, { score: "1.40", tier: "Tier-1", regionKey: 0, regionName: "Rest of World (ISO)" }, { score: "1.90", tier: "Tier-2", regionKey: 0, regionName: "Rest of World (ISO)" }, { score: "2.40", tier: "Tier-2", regionKey: 12, regionName: "Northern America" }, { score: "2.20", tier: "Tier-2", regionKey: 22, regionName: "Southern Europe" }, { score: "2.30", tier: "Tier-2", regionKey: 20, regionName: "Eastern Europe" }, { score: "1.80", tier: "Tier-2", regionKey: 10, regionName: "Central America" }, { score: "2.20", tier: "Tier-2", regionKey: 20, regionName: "Eastern Europe" }, { score: "1.80", tier: "Tier-2", regionKey: 22, regionName: "Southern Europe" }, { score: "2.60", tier: "Tier-3", regionKey: 65, regionName: "Eastern Europe" }];
homes.sort(function (a, b) {
function getDecimal(s) { return s.match(/d+/); };
return getDecimal(a.tier) - getDecimal(b.tier) || b.score - a.score;
});
console.log(homes);
.as-console-wrapper { max-height: 100% !important; top: 0; }
另一种可能性是将String#localeCompare
与options
灵敏度
哪些差异应导致非零结果值。可能的值是:
"base"
:仅在基本上不同的字符串与不等式相比。示例:a ≠ b
,a = á
,a = A
。"accent"
:仅在基本字母或口音和其他变性标记上有所不同的字符串与不平等相比。示例:a ≠ b
,a ≠ á
,a = A
。"case"
:仅基本字母或案例不同的字符串与不等式相比。示例:a ≠ b
,a = á
,a ≠ A
。"variant"
:基本字母,重音和其他变性标记的字符串或案例与不平等相比。也可以考虑其他差异。示例:a ≠ b
,a ≠ á
,a ≠ A
。默认值是"变体"用于使用" sort&quot";它依赖于用法"搜索"。
数字
是否应该使用数字整理,以便" 1"&lt;2&quot&quot&lt;" 10"。可能的值是
true
和false
;默认值为false
。可以通过选项属性或Unicode扩展密钥设置此选项;如果提供两者,则options
属性优先。不需要实施来支持此属性。
var homes = [{ score: "1.40", tier: "Tier-10", regionKey: 12, regionName: "Northern America" }, { score: "1.40", tier: "Tier-1", regionKey: 21, regionName: "Northern Europe" }, { score: "1.40", tier: "Tier-1", regionKey: 0, regionName: "Rest of World (ISO)" }, { score: "1.90", tier: "Tier-2", regionKey: 0, regionName: "Rest of World (ISO)" }, { score: "2.40", tier: "Tier-2", regionKey: 12, regionName: "Northern America" }, { score: "2.20", tier: "Tier-2", regionKey: 22, regionName: "Southern Europe" }, { score: "2.30", tier: "Tier-2", regionKey: 20, regionName: "Eastern Europe" }, { score: "1.80", tier: "Tier-2", regionKey: 10, regionName: "Central America" }, { score: "2.20", tier: "Tier-2", regionKey: 20, regionName: "Eastern Europe" }, { score: "1.80", tier: "Tier-2", regionKey: 22, regionName: "Southern Europe" }, { score: "2.60", tier: "Tier-3", regionKey: 65, regionName: "Eastern Europe" }];
homes.sort(function (a, b) {
return a.tier.localeCompare(b.tier, undefined, { numeric: true, sensitivity: 'base' }) || b.score - a.score;
});
console.log(homes);
.as-console-wrapper { max-height: 100% !important; top: 0; }