Use ascending param to decide how to order infinite values

This commit is contained in:
Victoria A
2023-10-21 17:13:46 +01:00
parent c4ac592555
commit df13d1b080
4 changed files with 22 additions and 20 deletions
+3 -2
View File
@@ -28,7 +28,7 @@ interface DataGridColumn<T> {
field: keyof T
label: string
sortable?: boolean
less?: (a: T, b: T) => number
less?: (a: T, b: T, ascending: boolean) => number
filterable?: boolean
toCellValue?: (rowIndex: number) => string | React.ReactNode
padding?: "normal" | "checkbox" | "none"
@@ -358,7 +358,8 @@ function stableSort<T>(
const stabilizedThis = array.map((el, index) => [el, index] as [T, number])
stabilizedThis.sort((a, b) => {
if (less) {
const result = order == "asc" ? -less(a[0], b[0]) : less(a[0], b[0])
const ascending = order == "asc"
const result = ascending ? -less(a[0], b[0], ascending) : less(a[0], b[0], ascending)
if (result !== 0) return result
} else {
const result = comparator(a[0], b[0])
@@ -28,7 +28,7 @@ export const TrialTable: FC<{
field: "values",
label: "Value",
sortable: true,
less: (firstEl, secondEl): number => {
less: (firstEl, secondEl, ascending): number => {
const firstVal = firstEl.values?.[0]
const secondVal = secondEl.values?.[0]
@@ -36,9 +36,9 @@ export const TrialTable: FC<{
return 0
}
if (firstVal === undefined) {
return -1
return ascending ? -1 : 1
} else if (secondVal === undefined) {
return 1
return ascending ? 1 : -1
}
if (firstVal === "-inf" || secondVal === "inf") {
return 1
@@ -63,7 +63,7 @@ export const TrialTable: FC<{
? objectiveNames[objectiveId]
: `Objective ${objectiveId}`,
sortable: true,
less: (firstEl, secondEl): number => {
less: (firstEl, secondEl, ascending): number => {
const firstVal = firstEl.values?.[objectiveId]
const secondVal = secondEl.values?.[objectiveId]
@@ -71,9 +71,9 @@ export const TrialTable: FC<{
return 0
}
if (firstVal === undefined) {
return -1
return ascending ? -1 : 1
} else if (secondVal === undefined) {
return 1
return ascending ? 1 : -1
}
if (firstVal === "-inf" || secondVal === "inf") {
return 1
@@ -106,7 +106,7 @@ export const TrialTable: FC<{
?.param_external_value || null,
sortable: sortable,
filterable: filterable,
less: (firstEl, secondEl): number => {
less: (firstEl, secondEl, _): number => {
const firstVal = firstEl.params.find(
(p) => p.name === s.name
)?.param_internal_value
@@ -146,7 +146,7 @@ export const TrialTable: FC<{
?.value || null,
sortable: attr_spec.sortable,
filterable: !attr_spec.sortable,
less: (firstEl, secondEl): number => {
less: (firstEl, secondEl, _): number => {
const firstVal = firstEl.user_attrs.find(
(attr) => attr.key === attr_spec.key
)?.value
+3 -2
View File
@@ -28,7 +28,7 @@ interface DataGridColumn<T> {
field: keyof T
label: string
sortable?: boolean
less?: (a: T, b: T) => number
less?: (a: T, b: T, ascending: boolean) => number
filterable?: boolean
toCellValue?: (rowIndex: number) => string | React.ReactNode
padding?: "normal" | "checkbox" | "none"
@@ -358,7 +358,8 @@ function stableSort<T>(
const stabilizedThis = array.map((el, index) => [el, index] as [T, number])
stabilizedThis.sort((a, b) => {
if (less) {
const result = order == "asc" ? -less(a[0], b[0]) : less(a[0], b[0])
const ascending = order == "asc"
const result = ascending ? -less(a[0], b[0], ascending) : less(a[0], b[0], ascending)
if (result !== 0) return result
} else {
const result = comparator(a[0], b[0])
+8 -8
View File
@@ -25,7 +25,7 @@ export const TrialTable: FC<{
field: "values",
label: "Value",
sortable: true,
less: (firstEl, secondEl): number => {
less: (firstEl, secondEl, ascending): number => {
const firstVal = firstEl.values?.[0]
const secondVal = secondEl.values?.[0]
@@ -33,9 +33,9 @@ export const TrialTable: FC<{
return 0
}
if (firstVal === undefined) {
return -1
return ascending ? -1 : 1
} else if (secondVal === undefined) {
return 1
return ascending ? 1 : -1
}
if (firstVal === "-inf" || secondVal === "inf") {
return 1
@@ -57,7 +57,7 @@ export const TrialTable: FC<{
field: "values",
label: `Objective ${objectiveId}`,
sortable: true,
less: (firstEl, secondEl): number => {
less: (firstEl, secondEl, ascending): number => {
const firstVal = firstEl.values?.[objectiveId]
const secondVal = secondEl.values?.[objectiveId]
@@ -65,9 +65,9 @@ export const TrialTable: FC<{
return 0
}
if (firstVal === undefined) {
return -1
return ascending ? -1 : 1
} else if (secondVal === undefined) {
return 1
return ascending ? 1 : -1
}
if (firstVal === "-inf" || secondVal === "inf") {
return 1
@@ -96,7 +96,7 @@ export const TrialTable: FC<{
null,
sortable: true,
filterable: false,
less: (firstEl, secondEl): number => {
less: (firstEl, secondEl, _): number => {
const firstVal = firstEl.params.find(
(p) => p.name === s.name
)?.param_internal_value
@@ -126,7 +126,7 @@ export const TrialTable: FC<{
?.value || null,
sortable: attr_spec.sortable,
filterable: false,
less: (firstEl, secondEl): number => {
less: (firstEl, secondEl, _): number => {
const firstVal = firstEl.user_attrs.find(
(attr) => attr.key === attr_spec.key
)?.value