【问题标题】:Sorting Javascript first sort ascending then descending对Javascript进行排序首先升序然后降序
【发布时间】:2017-09-12 15:57:53
【问题描述】:

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

【问题讨论】:

  • 让我们试试 home.sort((a, b) => { return a.tier.localeCompare(b.tier) || b.score - a.score; })
  • 哪一列是降序/升序?
  • 分数列降序,层列升序
  • 感谢泰乐工作,请提供解释。
  • @aryan,它可能不适用于像 'Tier-10' 这样的字符串。这是在'Tier-2'之前排序的

标签: javascript arrays sorting compare


【解决方案1】:

将房屋声明下方的所有内容替换为;

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

根据您想要升序还是降序相应地翻转 。

【讨论】:

    【解决方案2】:

    您可以通过获取列值的差异来使用链式方法,如果前一个值为零,则获取下一个增量。

    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#localeCompareoptions 一起使用

    灵敏度

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

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

    使用“排序”的默认值为“变体”;它依赖于使用“搜索”的语言环境。

    数字

    是否应使用数字排序规则,例如“1”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; }

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 1970-01-01
      • 2013-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-03
      • 1970-01-01
      相关资源
      最近更新 更多