Skip to content

Commit

Permalink
fix: Ordering failing when one value is null (#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aviortheking authored May 18, 2024
1 parent 16fe072 commit d48971c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
17 changes: 17 additions & 0 deletions .bruno/fixes/489-crash-when-sorting-with-null-values.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
meta {
name: 489 - Crash When sorting with Null values
type: http
seq: 6
}

get {
url: {{BASE_URL}}/v2/en/cards?name=pikachu&sort:field=hp&sort:order=DESC
body: none
auth: none
}

query {
name: pikachu
sort:field: hp
sort:order: DESC
}
1 change: 1 addition & 0 deletions server/cards-database
Submodule cards-database added at c476d8
20 changes: 16 additions & 4 deletions server/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit d48971c

Please sign in to comment.