export const sortTable = (data, sortKey, direction) => {
const reverse = direction === "ascending" ? 1 : -1;
const so = data.sort((nameA, nameB) => {
const isString = Number.isNaN(Number(nameA)) // true => string
if (isString) {
if (nameA?.[sortKey]?.toUpperCase() < nameB?.[sortKey]?.toUpperCase()) {
return -1 * reverse;
}
if (nameA?.[sortKey]?.toUpperCase() > nameB?.[sortKey]?.toUpperCase()) {
return 1 * reverse;
}
// names must be equal
return 0;
} else {
return direction === "ascending" ? nameA?.[sortKey] - nameB?.[sortKey] : nameB?.[sortKey] - nameA?.[sortKey]
}
})
return so
}
Created
November 26, 2020 16:50
-
-
Save hungdev/6753a83202c377212e3cf40fcb107294 to your computer and use it in GitHub Desktop.
sort text and number
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@shunz19
It serves my application, because my data is Array object. So simple version is:
https://repl.it/@hungdev/InsubstantialFastScientist