Skip to content

Instantly share code, notes, and snippets.

@TravisMullen
Last active June 3, 2018 07:12
Show Gist options
  • Save TravisMullen/caa98e38698f954679a7809d8636605b to your computer and use it in GitHub Desktop.
Save TravisMullen/caa98e38698f954679a7809d8636605b to your computer and use it in GitHub Desktop.
The Good Sort.
/**
* 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
}
}
}
@TravisMullen
Copy link
Author

8 returns.

@TravisMullen
Copy link
Author

7 returns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment