【问题标题】:Function sorting list by 3 columns and initiating it with a modification函数按 3 列排序列表并通过修改启动它
【发布时间】:2017-12-27 07:36:35
【问题描述】:

我有一张表要排序。它目前有 3 列:

货币Td | accountNoTd |复选框Td

我想先按货币排序,然后按复选框(选中的在顶部),最后按帐号。但是,还需要进行一项修改 - 初始加载时,具有特定货币(“PLN”)的行应显示在表格顶部。之后所有剩余的行都应该正常排序。

我使用 list.js 对行进行排序。我写了以下排序函数:

const options = {
        valueNames: ['currencyTd', 'accountNoTd', 'checkboxTd']
    };

    const accountsList = new List('accountsList', options);

    accountsList.sort('currencyTd', {
        order: 'asc',            
        sortFunction: function (a, b) {  
            if ((a.currencyTd === 'PLN') != (b.currencyTd === 'PLN')) {
                return a.currencyTd === 'PLN' ? 1 : -1;
            }
            return a.currencyTd > b.currencyTd ? 1 :
                   a.currencyTd < b.currencyTd ? -1 : 0;
        }

    });

但它没有按我预期的方式工作。我可能会做错什么?

编辑:我忘了添加 - 我此时编写的当前函数(如上面的代码中所示)应该仅按货币列对其进行排序。当我实现它时,我想按其他列添加排序。

【问题讨论】:

    标签: javascript list sorting list.js


    【解决方案1】:

    您可以对所有排序条件使用链式方法。

    var array = [
            { currencyTd: 'DEF', accountNoTd: 2, checkboxTd: 0 },
            { currencyTd: 'ABC', accountNoTd: 2, checkboxTd: 1 },
            { currencyTd: 'PLN', accountNoTd: 2, checkboxTd: 1 },
            { currencyTd: 'ABC', accountNoTd: 2, checkboxTd: 0 },
            { currencyTd: 'PLN', accountNoTd: 3, checkboxTd: 0 },
            { currencyTd: 'DEF', accountNoTd: 2, checkboxTd: 1 },
            { currencyTd: 'DEF', accountNoTd: 3, checkboxTd: 0 },
            { currencyTd: 'PLN', accountNoTd: 3, checkboxTd: 1 },
            { currencyTd: 'ABC', accountNoTd: 1, checkboxTd: 0 },
            { currencyTd: 'ABC', accountNoTd: 1, checkboxTd: 1 },
            { currencyTd: 'PLN', accountNoTd: 2, checkboxTd: 0 },
            { currencyTd: 'PLN', accountNoTd: 1, checkboxTd: 0 },
            { currencyTd: 'ABC', accountNoTd: 3, checkboxTd: 1 },
            { currencyTd: 'DEF', accountNoTd: 1, checkboxTd: 0 },
            { currencyTd: 'ABC', accountNoTd: 3, checkboxTd: 0 },
            { currencyTd: 'DEF', accountNoTd: 1, checkboxTd: 1 },
            { currencyTd: 'PLN', accountNoTd: 1, checkboxTd: 1 },
            { currencyTd: 'DEF', accountNoTd: 3, checkboxTd: 1 }
        ];
    
    array.sort(function (a, b) {
        return (
            (a.currencyTd !== 'PLN') - (b.currencyTd !== 'PLN') || // sort PLN to top
            a.currencyTd.localeCompare(b.currencyTd) ||            // sort currencyTd ASC
            a.accountNoTd - b.accountNoTd ||                       // sort accountNoTd ASC
            a.checkboxTd - b.checkboxTd                            // sort checkboxTd ASC
        );
    });
    console.log(array);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

    • 这很好,但仅适用于第一列。 'or' 运算符之后的下一行将被忽略。
    • 在哪里?更多?我看到在顶部'PLN' 之后按currencyTd 排序的结果。
    • 不要忘记将return后的部分用括号括起来,或者写一行,因为ASI
    • 对不起,我刚刚从我的字符串中获取了错误的值。
    • sortFunction: function (a, b) { return ( b._values.currencyTd.indexOf('PLN') - a._values.currencyTd.indexOf('PLN') || a._values. currencyTd.localeCompare(b._values.currencyTd) || b._values.checkboxTd.indexOf('checked') - a._values.checkboxTd.indexOf('checked') || b._values.accountNoTd.replace(/\s /g, '') > a._values.accountNoTd.replace(/\s/g, '') ? -1 : 1 ); }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 2016-11-27
    • 2020-09-06
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    • 2016-06-09
    相关资源
    最近更新 更多