Input Example:
[ '50-59', '60-69', '40-49', '>=70', '<40' ]
Expected Result
[ '<40', '40-49', '50-59', '60-69', '>=70' ]
Attempt; extended from my previous single-line (for debugging):
export function sort_ranges(ranges: string[]): string[] { const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base', ignorePunctuation: true }); return ranges.sort((a: string, b: string): number => { const bNaN: boolean = !isNaN(parseInt(b[0])); const col = () => console.info(`collator(${a}, ${b}) = ${collator.compare(a, b)}` ) || collator.compare(a, b); if (a[0] === '<' && bNaN) { console.info('< =', a); return -1; } else if (a[0] === '>' || b[0] === '>') { console.info('> =', a); return 1; } else return col(); } ); }
Runnable (mocha + chai in plnkr)
Note. Ranges are guaranteed not to overlap, and there may be other things in the array, such as "foo", that must be placed in any order at the end of the array.
Ideas: I could build a new array, for example [[50,59], ['<', '40']] , and then override the .sort method .sort , but that seems crazy. Is there a better solution?