From 8b3639db4bf09d70af7ea9b6c8316c3956bce986 Mon Sep 17 00:00:00 2001 From: Riley Jones Date: Tue, 24 Oct 2023 18:00:42 +0000 Subject: [PATCH] add undefined handling strategy --- .../runs/views/runs_table/sorting_utils.ts | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/tensorboard/webapp/runs/views/runs_table/sorting_utils.ts b/tensorboard/webapp/runs/views/runs_table/sorting_utils.ts index 0afed4c35fd..9e604d8c41b 100644 --- a/tensorboard/webapp/runs/views/runs_table/sorting_utils.ts +++ b/tensorboard/webapp/runs/views/runs_table/sorting_utils.ts @@ -5,6 +5,21 @@ import { } from '../../../widgets/data_table/types'; import {ExperimentAlias} from '../../../experiments/types'; +enum UndefinedStrategy { + BEFORE, + AFTER, +} + +interface SortOptions { + insertUndefined: UndefinedStrategy; +} + +const POTENTIALLY_NUMERIC = new Set(['string', 'number']); + +const DEFAULT_SORT_OPTIONS: SortOptions = { + insertUndefined: UndefinedStrategy.AFTER, +}; + export function parseNumericPrefix(value: string | number) { if (typeof value === 'number') { return isNaN(value) ? undefined : value; @@ -24,8 +39,6 @@ export function parseNumericPrefix(value: string | number) { return; } -const POTENTIALLY_NUMERIC = new Set(['string', 'number']); - export function sortTableDataItems( items: TableData[], sort: SortingInfo @@ -55,12 +68,14 @@ export function sortTableDataItems( ) { const aPrefix = parseNumericPrefix(aValue as string | number); const bPrefix = parseNumericPrefix(bValue as string | number); - // Show runs with numbers prior to runs without numbers + // Show runs with numbers before to runs without numbers if ( (aPrefix === undefined || bPrefix === undefined) && aPrefix !== bPrefix ) { - return orderFromLocalComparison(aPrefix, bPrefix); + return orderFromLocalComparison(aPrefix, bPrefix, { + insertUndefined: UndefinedStrategy.BEFORE, + }); } if (aPrefix !== undefined && bPrefix !== undefined) { if (aPrefix === bPrefix) { @@ -68,7 +83,9 @@ export function sortTableDataItems( aValue.toString().slice(aPrefix.toString().length) || undefined; const bPostfix = bValue.toString().slice(bPrefix.toString().length) || undefined; - return orderFromLocalComparison(aPostfix, bPostfix); + return orderFromLocalComparison(aPostfix, bPostfix, { + insertUndefined: UndefinedStrategy.BEFORE, + }); } return orderFromLocalComparison(aPrefix, bPrefix); @@ -81,17 +98,18 @@ export function sortTableDataItems( function orderFromLocalComparison( a: TableData[string] | undefined, - b: TableData[string] | undefined + b: TableData[string] | undefined, + {insertUndefined}: SortOptions = DEFAULT_SORT_OPTIONS ) { if (a === b) { return 0; } if (a === undefined) { - return 1; + return insertUndefined === UndefinedStrategy.AFTER ? 1 : -1; } if (b === undefined) { - return -1; + return insertUndefined === UndefinedStrategy.AFTER ? -1 : 1; } return a < b === (sort.order === SortingOrder.ASCENDING) ? -1 : 1;