import {Need} from '../../models/Need'; interface sortAlgo { (a: Need, b: Need): number; } // sort functions const sortByName: sortAlgo = (a: Need, b: Need): number => { if(a.name.toLocaleLowerCase() < b.name.toLocaleLowerCase()) { return -1; } return 1; } const sortByGoal: sortAlgo = (a: Need, b: Need): number => { if(a.maxGoal == b.maxGoal) { return sortByName(a,b); } else if(a.maxGoal > b.maxGoal) { return -1; } return 1; } const sortByCompletion: sortAlgo = (a: Need, b: Need): number => { if(a.current == b.current) { return sortByGoal(a,b); } else if(a.current > b.current) { return -1; } return 1; } const sortByPriority: sortAlgo = (a: Need, b: Need): number => { if(a.urgent == b.urgent) { return sortByGoal(a,b); } else if(a.urgent && !b.urgent) { return -1; } return 1; } const sortByLocation: sortAlgo = (a: Need, b: Need): number => { if(a.location.toLocaleLowerCase() < b.location.toLocaleLowerCase()) { return -1; } return 1; } export const SortingAlgoArrays: {[key: string]: { func: sortAlgo, display: [string, string]}} = { "sortByPriority": { func: sortByPriority, display: ["Highest Priority", "Lowest Priority" ] }, "sortByName": { func: sortByName, display: ["Name (A to Z)", "Name (Z to A)" ] }, "sortByLocation": { func: sortByLocation, display: ["Location (A to Z)", "Location (Z to A)" ] }, "sortByCompletion": { func: sortByCompletion, display: ["Most Completed", "Least Completed" ] }, "sortByGoal": { func: sortByGoal, display: ["Largest Maximum Goal", "Smallest Maximum Goal" ] }, };