简体   繁体   English

排序Javascript先升序再降序

[英]Sorting Javascript first sort ascending then descending

Javascript array sort Compare function issue, I created the below array need to sort one column as descending and keep the one column as ascending, Use simple compare function but the result not proper. Javascript数组排序比较功能问题,我创建了以下数组,需要将一列降序排列并保持一列升序,使用简单的比较功能,但结果不正确。 Its great extend to help me this issue, 它极大地帮助了我这个问题,

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

You could use a chained approach by getting the difference of the column values and get the next delty if the previous value is zero. 您可以通过获取列值的差值来使用链式方法,如果前一个值为零,则可以获取下一个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; } 

An other possibility is to use String#localeCompare with with options 另一种可能性是将String#localeCompareoptions一起使用

sensitivity 灵敏度

Which differences in the strings should lead to non-zero result values. 字符串中的哪些差异应导致非零结果值。 Possible values are: 可能的值为:

  • "base" : Only strings that differ in base letters compare as unequal. "base" :只有基本字母不同的字符串才能比较为不相等。 Examples: a ≠ b , a = á , a = A . 例如: a ≠ ba = áa = A
  • "accent" : Only strings that differ in base letters or accents and other diacritic marks compare as unequal. "accent" :仅将基本字母或重音与其他音标不同的字符串视为不相等。 Examples: a ≠ b , a ≠ á , a = A . 例如: a ≠ ba ≠ áa = A
  • "case" : Only strings that differ in base letters or case compare as unequal. "case" :仅基本字母或大小写不同的字符串比较为不相等。 Examples: a ≠ b , a = á , a ≠ A . 例如: a ≠ ba = áa ≠ A
  • "variant" : Strings that differ in base letters, accents and other diacritic marks, or case compare as unequal. "variant" :基本字母,重音符号和其他变音符号或大小写不同的字符串比较不相等。 Other differences may also be taken into consideration. 也可以考虑其他差异。 Examples: a ≠ b , a ≠ á , a ≠ A . 示例: a ≠ ba ≠ áa ≠ A

The default is "variant" for usage "sort"; 用法“ sort”的默认值为“ variant”; it's locale dependent for usage "search". 它取决于使用情况“搜索”的语言环境。

numeric 数字

Whether numeric collation should be used, such that "1" < "2" < "10". 是否应使用数字排序规则,例如“ 1” <“ 2” <“ 10”。 Possible values are true and false ; 可能的值是truefalse the default is false . 默认值为false This option can be set through an options property or through a Unicode extension key; 可以通过options属性或Unicode扩展键设置此选项。 if both are provided, the options property takes precedence. 如果两者都提供,则options属性具有优先权。 Implementations are not required to support this property. 不需要实现来支持此属性。

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

Replace everything below the homes declaration with; 将房屋声明下方的所有内容替换为;

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

flipping the < and > accordingly depending on whether you want ascending or descending order. 根据您要的升序还是降序相应地翻转<和>。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM