|
| 1 | +/* Copyright 2023 The TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | +import { |
| 16 | + SortingInfo, |
| 17 | + SortingOrder, |
| 18 | + TableData, |
| 19 | +} from '../../../widgets/data_table/types'; |
| 20 | +import {ExperimentAlias} from '../../../experiments/types'; |
| 21 | + |
| 22 | +enum UndefinedStrategy { |
| 23 | + BEFORE, |
| 24 | + AFTER, |
| 25 | +} |
| 26 | + |
| 27 | +interface SortOptions { |
| 28 | + insertUndefined: UndefinedStrategy; |
| 29 | +} |
| 30 | + |
| 31 | +const POTENTIALLY_NUMERIC_TYPES = new Set(['string', 'number']); |
| 32 | + |
| 33 | +const DEFAULT_SORT_OPTIONS: SortOptions = { |
| 34 | + insertUndefined: UndefinedStrategy.AFTER, |
| 35 | +}; |
| 36 | + |
| 37 | +export function parseNumericPrefix(value: string | number) { |
| 38 | + if (typeof value === 'number') { |
| 39 | + return isNaN(value) ? undefined : value; |
| 40 | + } |
| 41 | + |
| 42 | + if (!isNaN(parseInt(value))) { |
| 43 | + return parseInt(value); |
| 44 | + } |
| 45 | + |
| 46 | + for (let i = 0; i < value.length; i++) { |
| 47 | + if (isNaN(parseInt(value[i]))) { |
| 48 | + if (i === 0) return; |
| 49 | + return parseInt(value.slice(0, i)); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return; |
| 54 | +} |
| 55 | + |
| 56 | +export function sortTableDataItems( |
| 57 | + items: TableData[], |
| 58 | + sort: SortingInfo |
| 59 | +): TableData[] { |
| 60 | + const sortedItems = [...items]; |
| 61 | + |
| 62 | + sortedItems.sort((a, b) => { |
| 63 | + let aValue = a[sort.name]; |
| 64 | + let bValue = b[sort.name]; |
| 65 | + |
| 66 | + if (sort.name === 'experimentAlias') { |
| 67 | + aValue = (aValue as ExperimentAlias).aliasNumber; |
| 68 | + bValue = (bValue as ExperimentAlias).aliasNumber; |
| 69 | + } |
| 70 | + |
| 71 | + if (aValue === bValue) { |
| 72 | + return 0; |
| 73 | + } |
| 74 | + |
| 75 | + if (aValue === undefined || bValue === undefined) { |
| 76 | + return compareValues(aValue, bValue); |
| 77 | + } |
| 78 | + |
| 79 | + if ( |
| 80 | + POTENTIALLY_NUMERIC_TYPES.has(typeof aValue) && |
| 81 | + POTENTIALLY_NUMERIC_TYPES.has(typeof bValue) |
| 82 | + ) { |
| 83 | + const aPrefix = parseNumericPrefix(aValue as string | number); |
| 84 | + const bPrefix = parseNumericPrefix(bValue as string | number); |
| 85 | + // Show runs with numbers before to runs without numbers |
| 86 | + if ( |
| 87 | + (aPrefix === undefined || bPrefix === undefined) && |
| 88 | + aPrefix !== bPrefix |
| 89 | + ) { |
| 90 | + return compareValues(aPrefix, bPrefix, { |
| 91 | + insertUndefined: UndefinedStrategy.BEFORE, |
| 92 | + }); |
| 93 | + } |
| 94 | + if (aPrefix !== undefined && bPrefix !== undefined) { |
| 95 | + if (aPrefix === bPrefix) { |
| 96 | + const aPostfix = |
| 97 | + aValue.toString().slice(aPrefix.toString().length) || undefined; |
| 98 | + const bPostfix = |
| 99 | + bValue.toString().slice(bPrefix.toString().length) || undefined; |
| 100 | + return compareValues(aPostfix, bPostfix, { |
| 101 | + insertUndefined: UndefinedStrategy.BEFORE, |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + return compareValues(aPrefix, bPrefix); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return compareValues(aValue, bValue); |
| 110 | + }); |
| 111 | + return sortedItems; |
| 112 | + |
| 113 | + function compareValues( |
| 114 | + a: TableData[string] | undefined, |
| 115 | + b: TableData[string] | undefined, |
| 116 | + {insertUndefined}: SortOptions = DEFAULT_SORT_OPTIONS |
| 117 | + ) { |
| 118 | + if (a === b) { |
| 119 | + return 0; |
| 120 | + } |
| 121 | + |
| 122 | + if (a === undefined) { |
| 123 | + return insertUndefined === UndefinedStrategy.AFTER ? 1 : -1; |
| 124 | + } |
| 125 | + if (b === undefined) { |
| 126 | + return insertUndefined === UndefinedStrategy.AFTER ? -1 : 1; |
| 127 | + } |
| 128 | + |
| 129 | + return a < b === (sort.order === SortingOrder.ASCENDING) ? -1 : 1; |
| 130 | + } |
| 131 | +} |
0 commit comments