Skip to content

Commit 8b3639d

Browse files
committed
add undefined handling strategy
1 parent 91a36c7 commit 8b3639d

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

Diff for: tensorboard/webapp/runs/views/runs_table/sorting_utils.ts

+26-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@ import {
55
} from '../../../widgets/data_table/types';
66
import {ExperimentAlias} from '../../../experiments/types';
77

8+
enum UndefinedStrategy {
9+
BEFORE,
10+
AFTER,
11+
}
12+
13+
interface SortOptions {
14+
insertUndefined: UndefinedStrategy;
15+
}
16+
17+
const POTENTIALLY_NUMERIC = new Set(['string', 'number']);
18+
19+
const DEFAULT_SORT_OPTIONS: SortOptions = {
20+
insertUndefined: UndefinedStrategy.AFTER,
21+
};
22+
823
export function parseNumericPrefix(value: string | number) {
924
if (typeof value === 'number') {
1025
return isNaN(value) ? undefined : value;
@@ -24,8 +39,6 @@ export function parseNumericPrefix(value: string | number) {
2439
return;
2540
}
2641

27-
const POTENTIALLY_NUMERIC = new Set(['string', 'number']);
28-
2942
export function sortTableDataItems(
3043
items: TableData[],
3144
sort: SortingInfo
@@ -55,20 +68,24 @@ export function sortTableDataItems(
5568
) {
5669
const aPrefix = parseNumericPrefix(aValue as string | number);
5770
const bPrefix = parseNumericPrefix(bValue as string | number);
58-
// Show runs with numbers prior to runs without numbers
71+
// Show runs with numbers before to runs without numbers
5972
if (
6073
(aPrefix === undefined || bPrefix === undefined) &&
6174
aPrefix !== bPrefix
6275
) {
63-
return orderFromLocalComparison(aPrefix, bPrefix);
76+
return orderFromLocalComparison(aPrefix, bPrefix, {
77+
insertUndefined: UndefinedStrategy.BEFORE,
78+
});
6479
}
6580
if (aPrefix !== undefined && bPrefix !== undefined) {
6681
if (aPrefix === bPrefix) {
6782
const aPostfix =
6883
aValue.toString().slice(aPrefix.toString().length) || undefined;
6984
const bPostfix =
7085
bValue.toString().slice(bPrefix.toString().length) || undefined;
71-
return orderFromLocalComparison(aPostfix, bPostfix);
86+
return orderFromLocalComparison(aPostfix, bPostfix, {
87+
insertUndefined: UndefinedStrategy.BEFORE,
88+
});
7289
}
7390

7491
return orderFromLocalComparison(aPrefix, bPrefix);
@@ -81,17 +98,18 @@ export function sortTableDataItems(
8198

8299
function orderFromLocalComparison(
83100
a: TableData[string] | undefined,
84-
b: TableData[string] | undefined
101+
b: TableData[string] | undefined,
102+
{insertUndefined}: SortOptions = DEFAULT_SORT_OPTIONS
85103
) {
86104
if (a === b) {
87105
return 0;
88106
}
89107

90108
if (a === undefined) {
91-
return 1;
109+
return insertUndefined === UndefinedStrategy.AFTER ? 1 : -1;
92110
}
93111
if (b === undefined) {
94-
return -1;
112+
return insertUndefined === UndefinedStrategy.AFTER ? -1 : 1;
95113
}
96114

97115
return a < b === (sort.order === SortingOrder.ASCENDING) ? -1 : 1;

0 commit comments

Comments
 (0)