Skip to content
This repository was archived by the owner on Nov 12, 2025. It is now read-only.

Commit 40537f1

Browse files
dlabrecqdgutride
authored andcommitted
fix(sortPipe): sort order not guaranteed when all items are equal
1 parent 17a29af commit 40537f1

File tree

4 files changed

+9
-23
lines changed

4 files changed

+9
-23
lines changed

src/app/list/basic-list/list.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<!-- items -->
3535
<div class="list-pf-item {{item?.itemStyleClass}}"
3636
[ngClass]="{'active': item.selected || item.isItemExpanded}"
37-
*ngFor="let item of (config.usePinItems ? (items | sortArray: 'showPin': true) : items); let i = index">
37+
*ngFor="let item of (config.usePinItems ? (items | sortArray: 'showPin') : items); let i = index">
3838
<div class="list-pf-container">
3939
<!-- pin -->
4040
<div class="pfng-list-pin-container" *ngIf="config.usePinItems">

src/app/pipe/sort-array/examples/sort-array-example.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ <h5>Sort Array Descending by "name"</h5>
5353
<div class="col-sm-12">
5454
<h5>Sort Array Ascending by "name", Then Sort Descending by "pin"</h5>
5555
<div class="col-md-12"
56-
*ngFor="let item of (items3 | sortArray: 'name' | sortArray: 'pin': true)">
56+
*ngFor="let item of (items3 | sortArray: 'name' | sortArray: 'pin')">
5757
<div class="row">
5858
<div class="col-md-3">
5959
<span>{{item.name}}</span><strong *ngIf="item.pin"> (pinned)</strong>

src/app/pipe/sort-array/sort-array.pipe.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe('Sort array pipe', () => {
6363

6464
it('should sort array by "name", then sort array by "pin"', () => {
6565
let sortedItems = pipe.transform(items, 'name');
66-
sortedItems = pipe.transform(sortedItems, 'pin', true);
66+
sortedItems = pipe.transform(sortedItems, 'pin');
6767
expect(sortedItems[0].name).toBe('Holly Nichols');
6868
});
6969
});
Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Pipe, PipeTransform } from '@angular/core';
2-
import { isBoolean, isString } from 'util';
2+
3+
import { orderBy } from 'lodash';
34

45
/**
56
* Sort array pipe
@@ -21,25 +22,10 @@ export class SortArrayPipe implements PipeTransform {
2122
*/
2223
transform(arr: Array<any>, prop: any, descending: boolean = false): any {
2324
if (arr === undefined) {
24-
return;
25+
return arr;
2526
}
26-
const m = descending ? -1 : 1;
27-
return arr.sort((a: any, b: any): number => {
28-
let x = a[prop];
29-
let y = b[prop];
30-
31-
// Resolve undefined values for more predicable behavior
32-
if (x === undefined && isBoolean(y)) {
33-
x = false;
34-
} else if (x === undefined && isString(y)) {
35-
x = '';
36-
}
37-
if (y === undefined && isBoolean(x)) {
38-
y = false;
39-
} else if (y === undefined && isString(x)) {
40-
y = '';
41-
}
42-
return (x === y) ? 0 : (x < y) ? -1 * m : 1 * m;
43-
});
27+
const sortOrder = descending ? 'desc' : 'asc';
28+
const sortedArray = orderBy(arr, [prop], [sortOrder]);
29+
return sortedArray;
4430
}
4531
}

0 commit comments

Comments
 (0)