release/utils/sort.js
function sortRows(rows, columns, dirs) {
    if (!rows)
        return [];
    if (!dirs || !dirs.length || !columns)
        return rows.slice();
    /**
     * record the row ordering of results from prior sort operations (if applicable)
     * this is necessary to guarantee stable sorting behavior
     */
    var rowToIndexMap = new Map();
    rows.forEach(function (row, index) { return rowToIndexMap.set(row, index); });
    var temp = rows.slice();
    var cols = columns.reduce(function (obj, col) {
        if (col.comparator && typeof col.comparator === 'function') {
            obj[col.prop] = col.comparator;
        }
        return obj;
    }, {});
    // cache valueGetter and compareFn so that they
    // do not need to be looked-up in the sort function body
    var cachedDirs = dirs.map(function (dir) {
        var prop = dir.prop;
        return {
            prop: prop,
            dir: dir.dir,
            valueGetter: column_prop_getters_1.getterForProp(prop),
            compareFn: cols[prop] || orderByComparator
        };
    });
    return temp.sort(function (rowA, rowB) {
        for (var _i = 0, cachedDirs_1 = cachedDirs; _i < cachedDirs_1.length; _i++) {
            var cachedDir = cachedDirs_1[_i];
            // Get property and valuegetters for column to be sorted
            var prop = cachedDir.prop, valueGetter = cachedDir.valueGetter;
            // Get A and B cell values from rows based on properties of the columns
            var propA = valueGetter(rowA, prop);
            var propB = valueGetter(rowB, prop);
            // Compare function gets five parameters:
            // Two cell values to be compared as propA and propB
            // Two rows corresponding to the cells as rowA and rowB
            // Direction of the sort for this column as SortDirection
            // Compare can be a standard JS comparison function (a,b) => -1|0|1
            // as additional parameters are silently ignored. The whole row and sort
            // direction enable more complex sort logic.
            var comparison = cachedDir.dir !== types_1.SortDirection.desc ?
                cachedDir.compareFn(propA, propB, rowA, rowB, cachedDir.dir) :
                -cachedDir.compareFn(propA, propB, rowA, rowB, cachedDir.dir);
            // Don't return 0 yet in case of needing to sort by next property
            if (comparison !== 0)
                return comparison;
        }
        if (!(rowToIndexMap.has(rowA) && rowToIndexMap.has(rowB)))
            return 0;
        /**
         * all else being equal, preserve original order of the rows (stable sort)
         */
        return rowToIndexMap.get(rowA) < rowToIndexMap.get(rowB) ? -1 : 1;
    });
}
function sortRows(rows, columns, dirs) {
    if (!rows)
        return [];
    if (!dirs || !dirs.length || !columns)
        return rows.slice();
    /**
     * record the row ordering of results from prior sort operations (if applicable)
     * this is necessary to guarantee stable sorting behavior
     */
    var rowToIndexMap = new Map();
    rows.forEach(function (row, index) { return rowToIndexMap.set(row, index); });
    var temp = rows.slice();
    var cols = columns.reduce(function (obj, col) {
        if (col.comparator && typeof col.comparator === 'function') {
            obj[col.prop] = col.comparator;
        }
        return obj;
    }, {});
    // cache valueGetter and compareFn so that they
    // do not need to be looked-up in the sort function body
    var cachedDirs = dirs.map(function (dir) {
        var prop = dir.prop;
        return {
            prop: prop,
            dir: dir.dir,
            valueGetter: column_prop_getters_1.getterForProp(prop),
            compareFn: cols[prop] || orderByComparator
        };
    });
    return temp.sort(function (rowA, rowB) {
        for (var _i = 0, cachedDirs_1 = cachedDirs; _i < cachedDirs_1.length; _i++) {
            var cachedDir = cachedDirs_1[_i];
            // Get property and valuegetters for column to be sorted
            var prop = cachedDir.prop, valueGetter = cachedDir.valueGetter;
            // Get A and B cell values from rows based on properties of the columns
            var propA = valueGetter(rowA, prop);
            var propB = valueGetter(rowB, prop);
            // Compare function gets five parameters:
            // Two cell values to be compared as propA and propB
            // Two rows corresponding to the cells as rowA and rowB
            // Direction of the sort for this column as SortDirection
            // Compare can be a standard JS comparison function (a,b) => -1|0|1
            // as additional parameters are silently ignored. The whole row and sort
            // direction enable more complex sort logic.
            var comparison = cachedDir.dir !== types_1.SortDirection.desc ?
                cachedDir.compareFn(propA, propB, rowA, rowB, cachedDir.dir) :
                -cachedDir.compareFn(propA, propB, rowA, rowB, cachedDir.dir);
            // Don't return 0 yet in case of needing to sort by next property
            if (comparison !== 0)
                return comparison;
        }
        if (!(rowToIndexMap.has(rowA) && rowToIndexMap.has(rowB)))
            return 0;
        /**
         * all else being equal, preserve original order of the rows (stable sort)
         */
        return rowToIndexMap.get(rowA) < rowToIndexMap.get(rowB) ? -1 : 1;
    });
}
function sortRows ( rows , columns , dirs ) { if ( ! rows ) return [ ] ; if ( ! dirs || ! dirs . length || ! columns ) return rows . slice ( ) ; /**
     * record the row ordering of results from prior sort operations (if applicable)
     * this is necessary to guarantee stable sorting behavior
     */ var rowToIndexMap = new Map ( ) ; rows . forEach ( function ( row , index ) { return rowToIndexMap . set ( row , index ) ; } ) ; var temp = rows . slice ( ) ; var cols = columns . reduce ( function ( obj , col ) { if ( col . comparator && typeof col . comparator === 'function' ) { obj [ col . prop ] = col . comparator ; } return obj ; } , { } ) ; // cache valueGetter and compareFn so that they // do not need to be looked-up in the sort function body var cachedDirs = dirs . map ( function ( dir ) { var prop = dir . prop ; return { prop : prop , dir : dir . dir , valueGetter : column_prop_getters_1 . getterForProp ( prop ) , compareFn : cols [ prop ] || orderByComparator } ; } ) ; return temp . sort ( function ( rowA , rowB ) { for ( var _i = 0 , cachedDirs_1 = cachedDirs ; _i < cachedDirs_1 . length ; _i ++ ) { var cachedDir = cachedDirs_1 [ _i ] ; // Get property and valuegetters for column to be sorted var prop = cachedDir . prop , valueGetter = cachedDir . valueGetter ; // Get A and B cell values from rows based on properties of the columns var propA = valueGetter ( rowA , prop ) ; var propB = valueGetter ( rowB , prop ) ; // Compare function gets five parameters: // Two cell values to be compared as propA and propB // Two rows corresponding to the cells as rowA and rowB // Direction of the sort for this column as SortDirection // Compare can be a standard JS comparison function (a,b) => -1|0|1 // as additional parameters are silently ignored. The whole row and sort // direction enable more complex sort logic. var comparison = cachedDir . dir !== types_1 . SortDirection . desc ? cachedDir . compareFn ( propA , propB , rowA , rowB , cachedDir . dir ) : - cachedDir . compareFn ( propA , propB , rowA , rowB , cachedDir . dir ) ; // Don't return 0 yet in case of needing to sort by next property if ( comparison !== 0 ) return comparison ; } if ( ! ( rowToIndexMap . has ( rowA ) && rowToIndexMap . has ( rowB ) ) ) return 0 ; /**
         * all else being equal, preserve original order of the rows (stable sort)
         */ return rowToIndexMap . get ( rowA ) < rowToIndexMap . get ( rowB ) ? - 1 : 1 ; } ) ; }
creates a shallow copy of the `rows` input and returns the sorted copy. this function
does not sort the `rows` argument in place
creates a shallow copy of the rows input and returns the sorted copy . this function does not sort the rows argument in place