import { ascend , descend , sort } from '@typed/list' ; import { Direction , Position , Tiles } from '../../model' ; import { isPositive , isVertical } from './directions' ; const MULTIPLIER = 10000 ; const convertPositionByRowToValue = ( [ column , row ] ) => row * MULTIPLIER + column ; const convertPositionByColumnToValue = ( [ column , row ] ) => column * MULTIPLIER + row ; export const sortTilesForDirection = ( direction , tiles ) => { const sortFn = isPositive ( direction ) ? descend : ascend ; const conversionFn = isVertical ( direction ) ? convertPositionByRowToValue : convertPositionByColumnToValue ; return sort ( sortFn ( tile => conversionFn ( tile . position ) ) , tiles ) ; } ;