Created
March 3, 2020 15:20
-
-
Save multivoltage/7122fa700fad8872dfcd7c221f7895a0 to your computer and use it in GitHub Desktop.
a sort function bases on most used entries
This file contains hidden or 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
const MOST_USED = ["BASE", "INTERMEDI", "REJECTED", "FULL"] // ["BASE", "INTERMEDI", "REJECTED", "FULL"]; | |
const list = ["super","base wire","base","START","PREMIUM","PLUS","Intermedi","Blocked","Advanced"] // ["REJECTED",",ciccio","FULL","INTERMEDI","BASE"] | |
function beautifulSort(a,b){ | |
const i1 = MOST_USED.indexOf(a.toUpperCase()) | |
const i2 = MOST_USED.indexOf(b.toUpperCase()) | |
if(MOST_USED.includes(a.toUpperCase()) && MOST_USED.includes(b.toUpperCase)){ | |
return i1-i2 | |
} | |
if(MOST_USED.includes(a.toUpperCase()) && !MOST_USED.includes(b.toUpperCase())){ | |
return -1 | |
} | |
if(!MOST_USED.includes(a.toUpperCase()) && MOST_USED.includes(b.toUpperCase())){ | |
return 1 | |
} | |
return a<b | |
} | |
console.log(list.sort(beautifulSort)) | |
/** | |
* produce: -> [ 'base', 'Intermedi', 'super', 'base wire', 'START', 'PREMIUM', 'PLUS', 'Blocked', 'Advanced' ] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment