param arr param field returns { string } export const getSortType = ( arr , field ) : string => { let type = "" ; for ( const doc of arr ) { if ( doc . hasOwnProperty ( field ) && doc [ field ] !== null && doc [ field ] !== undefined ) { type = Object . prototype . toString . call ( doc [ field ] ) ; break ; } } return type ; } ; const merge = ( left , right , sortField , sort , type ) => { const result = [ ] ; const leftLength = left . length ; const rightLength = right . length ; let l = 0 ; let r = 0 ; if ( type === "[object Date]" || type === "[object Number]" || type === "[object String]" ) { if ( sort === - 1 ) { while ( l < leftLength && r < rightLength ) { if ( left [ l ] [ sortField ] > right [ r ] [ sortField ] ) { result . push ( left [ l ++ ] ) ; } else { result . push ( right [ r ++ ] ) ; } } } else if ( sort === 1 ) { while ( l < leftLength && r < rightLength ) { if ( left [ l ] [ sortField ] < right [ r ] [ sortField ] ) { result . push ( left [ l ++ ] ) ; } else { result . push ( right [ r ++ ] ) ; } } } } else { throw new Error ( `Sortable types are [object Date], [object String], and [object Number], this is not a sortable field, ${ Object . prototype . toString . call ( type ) }` ) ; } return result . concat ( left . slice ( l ) ) . concat ( right . slice ( r ) ) ; } ; param toSort param sortField param sortParam param type export const mergeSort = ( toSort , sortField , sortParam , type ) : any => { const len = toSort . length ; if ( len < 2 ) { return toSort ; } const mid = Math . floor ( len / 2 ) ; const left = toSort . slice ( 0 , mid ) ; const right = toSort . slice ( mid ) ; return merge ( mergeSort ( left , sortField , sortParam , type ) , mergeSort ( right , sortField , sortParam , type ) , sortField , sortParam , type ) ; } ;