import { TrackModel } from '../database/TracksDatabase' ; import orderBy from 'lodash/orderBy' ; import { DraftArray } from 'immer' ; param { TrackModel [ ] } List returns { TrackModel [ ] } export function sortTracks ( sortBy , sortDirection , library ) { if ( ! sortBy . localeCompare ( 'title' ) ) { let tracks = library . sort ( ( a , b ) => a . common . title . localeCompare ( b . common . title ) ) ; if ( sortDirection === 'DESC' ) tracks . reverse ( ) ; return tracks ; } else if ( ! sortBy . localeCompare ( 'Added At' ) || ! sortBy . localeCompare ( 'Modified At' ) ) { let tracks = ( orderBy ( library , [ ( item ) => sortBy . localeCompare ( 'Added At' ) ? item . stats . ctimeMs : item . stats . mtimeMs , ( item ) => item . common . title , ] , [ sortDirection , 'ASC' ] ) as any ) as TrackModel [ ] ; if ( sortDirection === 'DESC' ) tracks . reverse ( ) ; return tracks ; } else { let tracks = ( orderBy ( library , [ ( item ) => item . common [ sortBy ] , ( item ) => item . common . title , ] , [ sortDirection , 'ASC' ] ) as any ) as TrackModel [ ] ; if ( sortDirection === 'DESC' ) tracks . reverse ( ) ; return tracks ; } }