Skip to content

Commit

Permalink
fix: snovakovic#62 — can sort on multiple types in an array
Browse files Browse the repository at this point in the history
  • Loading branch information
mesqueeb committed Aug 20, 2022
1 parent 0e26e94 commit 6e3fcd5
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ const unpackObjectSorter = function(sortByObj:ISortByObjectSorter<any>) {
return { order, sortBy, comparer };
};

/**
* Returns the object type of the given payload
*
* @param {*} payload
* @returns {string}
*/
const getType = function(payload:any):string {
return Object.prototype.toString.call(payload).slice(8, -1);
};

// >>> SORTERS <<<

const multiPropertySorterProvider = function(defaultComparer:IComparer) {
Expand Down Expand Up @@ -215,8 +225,16 @@ export function createNewSortInstance(opts:ISortInstanceOptions):<T>(_ctx:T[])=>
export const defaultComparer = (a, b, order):number => {
if (a == null) return order;
if (b == null) return -order;
if (a < b) return -1;
if (a > b) return 1;
const aType = getType(a);
const bType = getType(b);
if (aType === bType) {
if (a < b) return -1;
if (a > b) return 1;

return 0;
}
if (aType < bType) return -1;
if (aType > bType) return 1;

return 0;
};
Expand Down

0 comments on commit 6e3fcd5

Please sign in to comment.