UNPKG

linq-extensions

Version:

Linq-like extension methods for JavaScript and TypeScript builtin collections

43 lines 1.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Ordering = void 0; class Ordering { constructor(selector, comparator, direction) { this.selector = selector; this.comparator = comparator; if (direction === 'desc') { this.comparator = (left, right) => comparator(right, left); } } execute(iterable) { const array = iterable.toArray(); this.quickSort(array, 0, array.length - 1); return array; } quickSort(array, start, end) { if (start < end) { const partitionIndex = this.partition(array, start, end); this.quickSort(array, start, partitionIndex - 1); this.quickSort(array, partitionIndex + 1, end); } } partition(array, start, end) { let pivot = this.selector(array[end]); let i = start - 1; for (let j = start; j < end; j++) { if (this.comparator(this.selector(array[j]), pivot) < 0) { i++; this.swap(array, i, j); } } this.swap(array, i + 1, end); return i + 1; } swap(array, i, j) { let tmp = array[i]; array[i] = array[j]; array[j] = tmp; } } exports.Ordering = Ordering; //# sourceMappingURL=ordering.js.map