sort.js (3306B)
1 import { arraySize as size } from '../../utils/array.js'; 2 import { factory } from '../../utils/factory.js'; 3 var name = 'sort'; 4 var dependencies = ['typed', 'matrix', 'compare', 'compareNatural']; 5 export var createSort = /* #__PURE__ */factory(name, dependencies, _ref => { 6 var { 7 typed, 8 matrix, 9 compare, 10 compareNatural 11 } = _ref; 12 var compareAsc = compare; 13 14 var compareDesc = (a, b) => -compare(a, b); 15 /** 16 * Sort the items in a matrix. 17 * 18 * Syntax: 19 * 20 * math.sort(x) 21 * math.sort(x, compare) 22 * 23 * Examples: 24 * 25 * math.sort([5, 10, 1]) // returns [1, 5, 10] 26 * math.sort(['C', 'B', 'A', 'D'], math.compareNatural) 27 * // returns ['A', 'B', 'C', 'D'] 28 * 29 * function sortByLength (a, b) { 30 * return a.length - b.length 31 * } 32 * math.sort(['Langdon', 'Tom', 'Sara'], sortByLength) 33 * // returns ['Tom', 'Sara', 'Langdon'] 34 * 35 * See also: 36 * 37 * filter, forEach, map, compare, compareNatural 38 * 39 * @param {Matrix | Array} x A one dimensional matrix or array to sort 40 * @param {Function | 'asc' | 'desc' | 'natural'} [compare='asc'] 41 * An optional _comparator function or name. The function is called as 42 * `compare(a, b)`, and must return 1 when a > b, -1 when a < b, 43 * and 0 when a == b. 44 * @return {Matrix | Array} Returns the sorted matrix. 45 */ 46 47 48 return typed(name, { 49 Array: function Array(x) { 50 _arrayIsVector(x); 51 52 return x.sort(compareAsc); 53 }, 54 Matrix: function Matrix(x) { 55 _matrixIsVector(x); 56 57 return matrix(x.toArray().sort(compareAsc), x.storage()); 58 }, 59 'Array, function': function ArrayFunction(x, _comparator) { 60 _arrayIsVector(x); 61 62 return x.sort(_comparator); 63 }, 64 'Matrix, function': function MatrixFunction(x, _comparator) { 65 _matrixIsVector(x); 66 67 return matrix(x.toArray().sort(_comparator), x.storage()); 68 }, 69 'Array, string': function ArrayString(x, order) { 70 _arrayIsVector(x); 71 72 return x.sort(_comparator(order)); 73 }, 74 'Matrix, string': function MatrixString(x, order) { 75 _matrixIsVector(x); 76 77 return matrix(x.toArray().sort(_comparator(order)), x.storage()); 78 } 79 }); 80 /** 81 * Get the comparator for given order ('asc', 'desc', 'natural') 82 * @param {'asc' | 'desc' | 'natural'} order 83 * @return {Function} Returns a _comparator function 84 */ 85 86 function _comparator(order) { 87 if (order === 'asc') { 88 return compareAsc; 89 } else if (order === 'desc') { 90 return compareDesc; 91 } else if (order === 'natural') { 92 return compareNatural; 93 } else { 94 throw new Error('String "asc", "desc", or "natural" expected'); 95 } 96 } 97 /** 98 * Validate whether an array is one dimensional 99 * Throws an error when this is not the case 100 * @param {Array} array 101 * @private 102 */ 103 104 105 function _arrayIsVector(array) { 106 if (size(array).length !== 1) { 107 throw new Error('One dimensional array expected'); 108 } 109 } 110 /** 111 * Validate whether a matrix is one dimensional 112 * Throws an error when this is not the case 113 * @param {Matrix} matrix 114 * @private 115 */ 116 117 118 function _matrixIsVector(matrix) { 119 if (matrix.size().length !== 1) { 120 throw new Error('One dimensional matrix expected'); 121 } 122 } 123 });