Last active
June 3, 2018 07:12
-
-
Save TravisMullen/caa98e38698f954679a7809d8636605b to your computer and use it in GitHub Desktop.
The Good Sort.
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
/** | |
* The Good Sort. When your object attribute [values] are shady AF. | |
* @param {any} valueA | |
* @param {any} valueB | |
* @param {Boolean} descending - True for descending. Default is [false] ascending. | |
*/ | |
export default function sortJawn (valueA, valueB, descending = false) { | |
// the same... carry on | |
if (valueA === valueB) { | |
return 0 | |
} | |
// check orientation. | |
const order = [1] | |
const otherDirection = -1 | |
if (descending) { | |
order.unshift(otherDirection) | |
} else { | |
order.push(otherDirection) | |
} | |
// always weight bad data lower | |
// check individually | |
// let zero rank higher than falsey | |
if (!v1 && v1 !== 0) { | |
return order[0] | |
} | |
if (!v2 && v2 !== 0) { | |
return order[1] | |
} | |
// handle good [paired] data | |
if (typeof (v1) === 'string' && typeof (v2) === 'string') { | |
// - strings | |
if (v1 < v2) { | |
return order[1] | |
} else { // v1 < v2 :: cause of `if (valueA === valueB) {` on line 9 | |
return order[0] | |
} | |
} else { | |
// - [assumes] numbers | |
if (descending) { | |
return v2 - v1 | |
} else { | |
return v1 - v2 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
8 returns.