排序Javascript首先排序升序然后降序

时间:2017-09-12 15:57:54

标签: javascript arrays sorting compare

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);

2 个答案:

答案 0 :(得分:0)

您可以通过获取列值的差异来使用链式方法,并在前一个值为零时获取下一个delty。

&#13;
&#13;
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);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

另一种可能性是String#localeCompareoptions

一起使用
  

<强>灵敏度

     

字符串中的哪些差异应导致非零结果值。可能的值有:

     
      
  • "base":只有基本字母不同的字符串才会比较为不相等。示例:a ≠ ba = áa = A
  •   
  • "accent":只有基本字母或重音符号和其他变音符号不同的字符串才会比较为不相等。示例:a ≠ ba ≠ áa = A
  •   
  • "case":只有基本字母或大小写不同的字符串才会比较为不相等。示例:a ≠ ba = áa ≠ A
  •   
  • "variant":基本字母,重音符号和其他变音符号不同的字符串,或不相等的大小写比较。其他差异也可以考虑在内。示例:a ≠ ba ≠ áa ≠ A
  •   
     

默认为&#34;变体&#34;用法&#34;排序&#34 ;;它的区域设置取决于使用情况&#34;搜索&#34;。

     

<强>数字

     

是否应该使用数字校对,以便&#34; 1&#34; &LT; &#34; 2&#34; &LT; &#34; 10&#34 ;.可能的值为truefalse;默认值为false。可以通过options属性或Unicode扩展键设置此选项;如果两者都提供,则options属性优先。实现不需要支持此属性。

&#13;
&#13;
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);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 1 :(得分:0)

使用;

替换住宅声明下面的所有内容

return 0

翻转&lt;和&gt;因此,取决于您是想要升序还是降序。