1
0
mirror of https://github.com/tcgdex/cards-database.git synced 2025-06-14 00:29:19 +00:00

fix: Ordering failing when one value is null (#490)

This commit is contained in:
2024-05-19 01:32:23 +02:00
committed by GitHub
parent 16fe072e7e
commit d48971c95e
3 changed files with 34 additions and 4 deletions

1
server/cards-database Submodule

Submodule server/cards-database added at c476d82618

View File

@ -150,9 +150,21 @@ export function handleSort(data: Array<any>, query: Query<any>) {
* @param order the base ordering
* @returns a function that is feed in the `sort` function
*/
const advSort = (a: string | number, b: string | number, order: 'ASC' | 'DESC' = 'ASC') => {
a = tryParse(a) ?? a
b = tryParse(b) ?? b
const advSort = (a?: string | number, b?: string | number, order: 'ASC' | 'DESC' = 'ASC') => {
const isANull = isNull(a)
const isBNull = isNull(b)
if (isANull && isBNull) {
return 0
}
if (isANull) {
return order === 'ASC' ? -1 : 1
}
if (isBNull) {
return order === 'ASC' ? 1 : -1
}
a = tryParse(a!) ?? a
b = tryParse(b!) ?? b
if (order === 'DESC') {
const tmp = a
@ -164,7 +176,7 @@ const advSort = (a: string | number, b: string | number, order: 'ASC' | 'DESC' =
return a - b
}
return a.toString().localeCompare(b.toString())
return a!.toString().localeCompare(b!.toString())
}
function tryParse(value: string | number): number | null {