|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +/** |
| 5 | + * Operations for sorting collections. |
| 6 | + * |
| 7 | + * @public |
| 8 | + */ |
| 9 | +export class Sort { |
| 10 | + /** |
| 11 | + * Compares `x` and `y` using the JavaScript `>` and `<` operators. This function is suitable for usage as |
| 12 | + * the callback for `array.sort()`. |
| 13 | + * @returns -1 if `x` is smaller than `y`, 1 if `x` is greater than `y`, or 0 if the values are equal. |
| 14 | + * |
| 15 | + * @example |
| 16 | + * |
| 17 | + * ```ts |
| 18 | + * let array: number[] = [3, 6, 2]; |
| 19 | + * array.sort(Sort.compareByValue); // [2, 3, 6] |
| 20 | + * ``` |
| 21 | + */ |
| 22 | + // tslint:disable-next-line:no-any |
| 23 | + public static compareByValue(x: any, y: any): number { |
| 24 | + if (x === y) { |
| 25 | + return 0; |
| 26 | + } |
| 27 | + if (x === undefined) { |
| 28 | + return -1; |
| 29 | + } |
| 30 | + if (x === null) { |
| 31 | + return -1; |
| 32 | + } |
| 33 | + if (x < y) { |
| 34 | + return -1; |
| 35 | + } |
| 36 | + if (x > y) { |
| 37 | + return 1; |
| 38 | + } |
| 39 | + return 0; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Sorts the array according to a key which is obtained from the array elements. |
| 44 | + * |
| 45 | + * @example |
| 46 | + * |
| 47 | + * ```ts |
| 48 | + * let array: string[] = [ 'ccc', 'bb', 'a' ]; |
| 49 | + * Sort.sortBy(array, x => x.length); // [ 'a', 'bb', 'ccc' ] |
| 50 | + * ``` |
| 51 | + */ |
| 52 | + // tslint:disable-next-line:no-any |
| 53 | + public static sortBy<T>(array: T[], keySelector: (element: T) => any, comparer: (x: any, y: any) => number |
| 54 | + = Sort.compareByValue): void { |
| 55 | + array.sort((x, y) => comparer(keySelector(x), keySelector(y))); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Returns true if the array is already sorted. |
| 60 | + */ |
| 61 | + // tslint:disable-next-line:no-any |
| 62 | + public static isSorted<T>(array: T[], comparer: (x: any, y: any) => number = Sort.compareByValue): boolean { |
| 63 | + let previous: T | undefined = undefined; |
| 64 | + for (const element of array) { |
| 65 | + if (comparer(previous, element) > 0) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + previous = element; |
| 69 | + } |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Returns true if the array is already sorted by the specified key. |
| 75 | + * |
| 76 | + * @example |
| 77 | + * |
| 78 | + * ```ts |
| 79 | + * let array: string[] = [ 'a', 'bb', 'ccc' ]; |
| 80 | + * Sort.isSortedBy(array, x => x.length); // true |
| 81 | + * ``` |
| 82 | + */ |
| 83 | + // tslint:disable-next-line:no-any |
| 84 | + public static isSortedBy<T>(array: T[], keySelector: (element: T) => any, comparer: (x: any, y: any) => number |
| 85 | + = Sort.compareByValue): boolean { |
| 86 | + |
| 87 | + let previousKey: T | undefined = undefined; |
| 88 | + for (const element of array) { |
| 89 | + const key: T = keySelector(element); |
| 90 | + if (comparer(previousKey, key) > 0) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + previousKey = key; |
| 94 | + } |
| 95 | + return true; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Sorts the entries in a Map object according to the keys. |
| 100 | + * |
| 101 | + * @example |
| 102 | + * |
| 103 | + * ```ts |
| 104 | + * let map: Map<string, number> = new Map<string, number>(); |
| 105 | + * map.set('zebra', 1); |
| 106 | + * map.set('goose', 2); |
| 107 | + * map.set('aardvark', 3); |
| 108 | + * Sort.sortMapKeys(map); |
| 109 | + * console.log(JSON.stringify(Array.from(map.keys()))); // ["aardvark","goose","zebra"] |
| 110 | + * ``` |
| 111 | + */ |
| 112 | + // tslint:disable-next-line:no-any |
| 113 | + public static sortMapKeys<K, V>(map: Map<K, V>, keyComparer: (x: K, y: K) => number = Sort.compareByValue): void { |
| 114 | + const pairs: [K, V][] = Array.from(map.entries()); |
| 115 | + |
| 116 | + // Sorting a map is expensive, so first check whether it's already sorted. |
| 117 | + if (Sort.isSortedBy(pairs, x => x[0], keyComparer)) { |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + Sort.sortBy(pairs, x => x[0], keyComparer); |
| 122 | + map.clear(); |
| 123 | + for (const pair of pairs) { |
| 124 | + map.set(pair[0], pair[1]); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments