@@ -5,6 +5,21 @@ import {
5
5
} from '../../../widgets/data_table/types' ;
6
6
import { ExperimentAlias } from '../../../experiments/types' ;
7
7
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
+
8
23
export function parseNumericPrefix ( value : string | number ) {
9
24
if ( typeof value === 'number' ) {
10
25
return isNaN ( value ) ? undefined : value ;
@@ -24,8 +39,6 @@ export function parseNumericPrefix(value: string | number) {
24
39
return ;
25
40
}
26
41
27
- const POTENTIALLY_NUMERIC = new Set ( [ 'string' , 'number' ] ) ;
28
-
29
42
export function sortTableDataItems (
30
43
items : TableData [ ] ,
31
44
sort : SortingInfo
@@ -55,20 +68,24 @@ export function sortTableDataItems(
55
68
) {
56
69
const aPrefix = parseNumericPrefix ( aValue as string | number ) ;
57
70
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
59
72
if (
60
73
( aPrefix === undefined || bPrefix === undefined ) &&
61
74
aPrefix !== bPrefix
62
75
) {
63
- return orderFromLocalComparison ( aPrefix , bPrefix ) ;
76
+ return orderFromLocalComparison ( aPrefix , bPrefix , {
77
+ insertUndefined : UndefinedStrategy . BEFORE ,
78
+ } ) ;
64
79
}
65
80
if ( aPrefix !== undefined && bPrefix !== undefined ) {
66
81
if ( aPrefix === bPrefix ) {
67
82
const aPostfix =
68
83
aValue . toString ( ) . slice ( aPrefix . toString ( ) . length ) || undefined ;
69
84
const bPostfix =
70
85
bValue . toString ( ) . slice ( bPrefix . toString ( ) . length ) || undefined ;
71
- return orderFromLocalComparison ( aPostfix , bPostfix ) ;
86
+ return orderFromLocalComparison ( aPostfix , bPostfix , {
87
+ insertUndefined : UndefinedStrategy . BEFORE ,
88
+ } ) ;
72
89
}
73
90
74
91
return orderFromLocalComparison ( aPrefix , bPrefix ) ;
@@ -81,17 +98,18 @@ export function sortTableDataItems(
81
98
82
99
function orderFromLocalComparison (
83
100
a : TableData [ string ] | undefined ,
84
- b : TableData [ string ] | undefined
101
+ b : TableData [ string ] | undefined ,
102
+ { insertUndefined} : SortOptions = DEFAULT_SORT_OPTIONS
85
103
) {
86
104
if ( a === b ) {
87
105
return 0 ;
88
106
}
89
107
90
108
if ( a === undefined ) {
91
- return 1 ;
109
+ return insertUndefined === UndefinedStrategy . AFTER ? 1 : - 1 ;
92
110
}
93
111
if ( b === undefined ) {
94
- return - 1 ;
112
+ return insertUndefined === UndefinedStrategy . AFTER ? - 1 : 1 ;
95
113
}
96
114
97
115
return a < b === ( sort . order === SortingOrder . ASCENDING ) ? - 1 : 1 ;
0 commit comments