Skip to content

Commit

Permalink
add undefined handling strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
rileyajones committed Oct 24, 2023
1 parent 91a36c7 commit 8b3639d
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions tensorboard/webapp/runs/views/runs_table/sorting_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -55,20 +68,24 @@ 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) {
const aPostfix =
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);
Expand All @@ -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;
Expand Down

0 comments on commit 8b3639d

Please sign in to comment.