UNPKG

@dgit/react-data-grid-addons

Version:

A set of addons for react-data-grid

42 lines (35 loc) 1.09 kB
import { utils } from 'react-data-grid'; const { isImmutableCollection } = utils; const getMixedTypeValueRetriever = (isImmutable) => { let retObj = {}; const retriever = (item, key) => { if(item[key] && item[key].sortValue) { return item[key].sortValue } return item[key]; }; const immutableRetriever = (immutable, key) => { return immutable.get(key); }; retObj.getValue = isImmutable ? immutableRetriever : retriever; return retObj; } const comparer = (a, b) => { if (a > b) { return 1; } else if (a < b) { return -1; } return 0; }; const sortRows = (rows, sortColumn, sortDirection) => { const retriever = getMixedTypeValueRetriever(isImmutableCollection(rows)); let sortDirectionSign = sortDirection === 'ASC' ? 1 : -1; let rowComparer = (a, b) => { return sortDirectionSign * comparer(retriever.getValue(a, sortColumn), retriever.getValue(b, sortColumn)); }; if (sortDirection === 'NONE') { return rows; } return rows.slice().sort(rowComparer); }; module.exports = sortRows; module.exports.comparer = comparer;