diff --git a/src/app/pipes/array/order-by.spec.ts b/src/app/pipes/array/order-by.spec.ts index 90859c2f..8338efa3 100644 --- a/src/app/pipes/array/order-by.spec.ts +++ b/src/app/pipes/array/order-by.spec.ts @@ -14,6 +14,17 @@ describe('OrderByPipe', () => { pipe = new OrderByPipe(); }); + it ('should return dates in order asc', () => { + const a = new Date(); + const b = new Date(); + expect(pipe.transform([a, b], '+')).toEqual([a, b]); + }); + + it ('should return numbers in order asc', () => { + const numbers = [0, -1, 345, 1234, 1337, -3]; + expect(pipe.transform(numbers, '+')).toEqual([-3, -1, 0, 345, 1234, 1337]); + }); + it('should not do anything in-case of not an array', () => { expect(pipe.transform('foo')).toEqual('foo'); expect(pipe.transform(null)).toEqual(null); diff --git a/src/app/pipes/array/order-by.ts b/src/app/pipes/array/order-by.ts index efbdd220..8377c5d3 100644 --- a/src/app/pipes/array/order-by.ts +++ b/src/app/pipes/array/order-by.ts @@ -31,8 +31,8 @@ export class OrderByPipe implements PipeTransform { if (config.length === 1) { switch (sign) { - case '+': return out.sort(); - case '-': return out.sort().reverse(); + case '+': return out.sort(OrderByPipe.simpleSort.bind(this)); + case '-': return out.sort(OrderByPipe.simpleSort.bind(this)).reverse(); } } @@ -40,14 +40,16 @@ export class OrderByPipe implements PipeTransform { } // default sort by value - return out.sort((a, b) => { - return isString(a) && isString(b) - ? a.toLowerCase().localeCompare(b.toLowerCase()) - : a - b; - }); + return out.sort(OrderByPipe.simpleSort.bind(this)); } - static orderCompare(prop: string, asc: boolean, a: any, b: any) { + private static simpleSort(a: any, b: any) { + return isString(a) && isString(b) + ? a.toLowerCase().localeCompare(b.toLowerCase()) + : a - b; + } + + private static orderCompare(prop: string, asc: boolean, a: any, b: any) { const first = extractDeepPropertyByMapKey(a, prop), second = extractDeepPropertyByMapKey(b, prop); @@ -65,7 +67,7 @@ export class OrderByPipe implements PipeTransform { : second - first; } - static extractFromConfig(config: any) { + private static extractFromConfig(config: any) { const sign = config.substr(0, 1); const prop = config.replace(/^[-+]/, ''); const asc = sign !== '-';