MCPcopy Create free account
hub / github.com/blacksmithgu/datacore / sort

Method sort

src/api/data-array.ts:287–303  ·  view source on GitHub ↗
(key?: ArrayFunc<T, U>, direction?: "asc" | "desc", comparator?: ArrayComparator<U>)

Source from the content-addressed store, hash-verified

285 }
286
287 public sort<U>(key?: ArrayFunc<T, U>, direction?: "asc" | "desc", comparator?: ArrayComparator<U>): DataArray<T> {
288 if (this.values.length == 0) return this;
289 let realComparator = comparator ?? this.defaultComparator;
290 let realKey = key ?? ((l: T) => l as unknown as U);
291
292 // Associate each entry with it's index for the key function, and then do a normal sort.
293 let copy = ([] as T[]).concat(this.array()).map((elem, index) => {
294 return { index: index, value: elem };
295 });
296 copy.sort((a, b) => {
297 let aKey = realKey(a.value, a.index, this.values);
298 let bKey = realKey(b.value, b.index, this.values);
299 return direction === "desc" ? -realComparator(aKey, bKey) : realComparator(aKey, bKey);
300 });
301
302 return this.lwrap(copy.map((e) => e.value));
303 }
304
305 public sortInPlace<U>(
306 key?: (value: T) => U,

Callers 1

groupByMethod · 0.95

Calls 5

arrayMethod · 0.95
lwrapMethod · 0.95
mapMethod · 0.65
concatMethod · 0.65
sortMethod · 0.65

Tested by

no test coverage detected