Skip to content

Commit

Permalink
Added compare function for orderBy and thenBy
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaifm committed Mar 11, 2020
1 parent 6d0f190 commit 5c3c1ae
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 17 deletions.
6 changes: 5 additions & 1 deletion linq.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ declare namespace Enumerable {
sequenceEqual(second: T[]): boolean;
sequenceEqual<TCompare>(second: T[], compareSelector: (element: T) => TCompare): boolean;
orderBy<TKey>(keySelector: (element: T) => TKey): IOrderedEnumerable<T>;
orderBy<TKey>(keySelector: (element: T) => TKey, comparer: (first: T, second: T) => number): IOrderedEnumerable<T>;
orderByDescending<TKey>(keySelector: (element: T) => TKey): IOrderedEnumerable<T>;
orderByDescending<TKey>(keySelector: (element: T) => TKey, comparer: (first: T, second: T) => number): IOrderedEnumerable<T>;
reverse(): IEnumerable<T>;
shuffle(): IEnumerable<T>;
weightedSample(weightSelector: (element: T) => number): IEnumerable<T>;
Expand Down Expand Up @@ -203,9 +205,11 @@ declare namespace Enumerable {
}

export interface IOrderedEnumerable<T> extends IEnumerable<T> {
createOrderedEnumerable<TKey>(keySelector: (element: T) => TKey, descending: boolean): IOrderedEnumerable<T>;
createOrderedEnumerable<TKey>(keySelector: (element: T) => TKey, comparer: (first: T, second: T) => number, descending: boolean): IOrderedEnumerable<T>;
thenBy<TKey>(keySelector: (element: T) => TKey): IOrderedEnumerable<T>;
thenBy<TKey>(keySelector: (element: T) => TKey, comparer: (first: T, second: T) => number): IOrderedEnumerable<T>;
thenByDescending<TKey>(keySelector: (element: T) => TKey): IOrderedEnumerable<T>;
thenByDescending<TKey>(keySelector: (element: T) => TKey, comparer: (first: T, second: T) => number): IOrderedEnumerable<T>;
}

export interface IDisposableEnumerable<T> extends IEnumerable<T> {
Expand Down
37 changes: 23 additions & 14 deletions linq.js
Original file line number Diff line number Diff line change
Expand Up @@ -1541,12 +1541,12 @@

/* Ordering Methods */

Enumerable.prototype.orderBy = function (keySelector) {
return new OrderedEnumerable(this, keySelector, false);
Enumerable.prototype.orderBy = function (keySelector, comparer) {
return new OrderedEnumerable(this, keySelector, comparer, false);
};

Enumerable.prototype.orderByDescending = function (keySelector) {
return new OrderedEnumerable(this, keySelector, true);
Enumerable.prototype.orderByDescending = function (keySelector, comparer) {
return new OrderedEnumerable(this, keySelector, comparer, true);
};

Enumerable.prototype.reverse = function () {
Expand Down Expand Up @@ -2501,24 +2501,27 @@

// private

var OrderedEnumerable = function (source, keySelector, descending, parent) {
var OrderedEnumerable = function (source, keySelector, comparer, descending, parent) {
this.source = source;
this.keySelector = Utils.createLambda(keySelector);
this.descending = descending;
this.parent = parent;

if (comparer)
this.comparer = Utils.createLambda(comparer);
};
OrderedEnumerable.prototype = new Enumerable();

OrderedEnumerable.prototype.createOrderedEnumerable = function (keySelector, descending) {
return new OrderedEnumerable(this.source, keySelector, descending, this);
OrderedEnumerable.prototype.createOrderedEnumerable = function (keySelector, comparer, descending) {
return new OrderedEnumerable(this.source, keySelector, comparer, descending, this);
};

OrderedEnumerable.prototype.thenBy = function (keySelector) {
return this.createOrderedEnumerable(keySelector, false);
OrderedEnumerable.prototype.thenBy = function (keySelector, comparer) {
return this.createOrderedEnumerable(keySelector, comparer, false);
};

OrderedEnumerable.prototype.thenByDescending = function (keySelector) {
return this.createOrderedEnumerable(keySelector, true);
OrderedEnumerable.prototype.thenByDescending = function (keySelector, comparer) {
return this.createOrderedEnumerable(keySelector, comparer, true);
};

OrderedEnumerable.prototype.getEnumerator = function () {
Expand Down Expand Up @@ -2549,15 +2552,19 @@
);
};

var SortContext = function (keySelector, descending, child) {
var SortContext = function (keySelector, comparer, descending, child) {
this.keySelector = keySelector;
this.descending = descending;
this.child = child;
this.comparer = comparer;
this.keys = null;
};

SortContext.create = function (orderedEnumerable, currentContext) {
var context = new SortContext(orderedEnumerable.keySelector, orderedEnumerable.descending, currentContext);
var context = new SortContext(
orderedEnumerable.keySelector, orderedEnumerable.comparer, orderedEnumerable.descending, currentContext
);

if (orderedEnumerable.parent != null) return SortContext.create(orderedEnumerable.parent, context);
return context;
};
Expand All @@ -2573,7 +2580,9 @@
};

SortContext.prototype.compare = function (index1, index2) {
var comparison = Utils.compare(this.keys[index1], this.keys[index2]);
var comparison = this.comparer ?
this.comparer(this.keys[index1], this.keys[index2]) :
Utils.compare(this.keys[index1], this.keys[index2]);

if (comparison == 0) {
if (this.child != null) return this.child.compare(index1, index2);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "linq",
"author": "Mihai Ciuraru <mihaixc@gmail.com>",
"description": "linq.js - LINQ for JavaScript library packaged for node.js",
"version": "3.2.1",
"version": "3.2.2",
"homepage": "https://github.com/mihaifm/linq",
"repository": {
"type": "git",
Expand Down
18 changes: 17 additions & 1 deletion test/ordering.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require("../extensions/linq.qunit.js")({'Enumerable': Enumerable});

module("Ordering");

var expected, actual; // will be removed
var expected, actual;

var list = [
{ a: 2, b: 4, c: 1 },
Expand All @@ -30,6 +30,11 @@ test("orderBy", function () {
deepEqual(actual, [1, 7, 31, 51, 51, 85, 99, 823]);

Enumerable.rangeTo(10, 1).orderBy("$%5").is(10, 5, 6, 1, 7, 2, 8, 3, 9, 4);

actual = ['b', 'a', 'd', 'c'];
deepEqual(Enumerable.from(actual).orderBy().toArray(), ['a', 'b', 'c', 'd']);
deepEqual(Enumerable.from(actual).orderBy(x=>x, "(x,y)=>x.localeCompare(y)").toArray(), ['a', 'b', 'c', 'd']);
deepEqual(Enumerable.from(actual).orderBy(x=>x, (x,y)=>x<y).toArray(), ['d', 'c', 'b', 'a']);
});

test("orderByDescending", function () {
Expand All @@ -39,6 +44,10 @@ test("orderByDescending", function () {
deepEqual(actual, [823, 99, 85, 51, 51, 31, 7, 1]);

Enumerable.rangeTo(1, 10).orderByDescending("$%5").is(4, 9, 3, 8, 2, 7, 1, 6, 5, 10);

actual = ['b', 'a', 'd', 'c'];
deepEqual(Enumerable.from(actual)
.orderByDescending(x=>x, (x, y)=>x < y ? -1 : +(x > y)).toArray(), ['d', 'c', 'b', 'a']);
});

test("thenBy", function () {
Expand Down Expand Up @@ -70,6 +79,13 @@ test("thenBy", function () {
{ a: "z", b: "e", c: "e" }
];
deepEqual(actual, expected);

actual = Enumerable.from(strlist)
.orderBy(l=>l.a)
.thenBy(l=>l, (x,y) => x.b < y.b ? -1 : x.b > y.b ? 1 : x.c < y.c ? -1 : +(x.c > y.c))
.toArray();

deepEqual(actual, expected);
});

test("thenByDescending", function () {
Expand Down

0 comments on commit 5c3c1ae

Please sign in to comment.