Created
March 18, 2020 05:56
-
-
Save dragg/3b2224d98b0b4edbe220e43181911c2b to your computer and use it in GitHub Desktop.
Sort exapmle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sort from './sort'; | |
function sort(type) { | |
const sortFunction = getSortFunctiion(type); | |
if(sortFunction) { | |
return products.sort(sortFunction); | |
} | |
return products; | |
} | |
function getSortFunction(type) { | |
let sortFunction = null; | |
switch (type) { | |
case 1: | |
sortFunction = sort.lessToMore('price') | |
break; | |
case 2: | |
sortFunction = sort.moreToLess('price') | |
break; | |
case 3: | |
sortFunction = sort.lessToMore('created_at', 'date) | |
break; | |
case 4: | |
sortFunction = sort.moreToLess('created_at', 'date') | |
break; | |
} | |
return sortFunction; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default { | |
moreToLess: (key, keyType) => { | |
return (a, b) => { | |
if(keyType === 'date') { | |
return new Date(b[key]) - new Date(a[key]); | |
} | |
return b[key] - a[key]; | |
}; | |
}, | |
lessToMore: (key) => { | |
return (a, b) => { | |
if(keyType === 'date') { | |
return new Date(b[key]) - new Date(a[key]); | |
} | |
return a[key] - b[key]; | |
}; | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment