export type SortBy = 'key' | 'value' ; export type SortDirection = 'asc' | 'desc' ; export function getSorter ( sortBy = 'key' ) { if ( sortBy === 'key' ) { return ( subject , sortDirection = 'asc' ) => { let sorted = [ ... subject . keys ( ) ] . sort ( ) ; if ( sortDirection === 'desc' ) { sorted . reverse ( ) ; } return new Map < string , number > ( sorted . map ( ( key ) => [ key , subject . get ( key ) ! ] ) ) ; } ; } return ( subject , sortDirection = 'desc' ) => { let sorted = [ ... subject . entries ( ) ] . sort ( ( a , b ) => a [ 1 ] - b [ 1 ] ) ; if ( sortDirection === 'desc' ) { sorted . reverse ( ) ; } return new Map < string , number > ( sorted ) ; } ; }