Pipe's array order changes even though data does not

51 views Asked by At

I have created the following pipe:

@Pipe({
  name: 'orderBy',
  pure: false
})
export class OrderByPipe implements PipeTransform {

  transform(array: Array<any>, orderProperty: string): Array<any> {
    array.sort((a: any, b: any) => {
      if (a[orderProperty] < b[orderProperty]) {
        return -1;
      } else if (a[orderProperty] > b[orderProperty]) {
        return 1;
      } else {
        return 0;
      }
    });
    return array;
  }

}

Usage:

<div class="patient-box level-1" draggable="true" *ngFor="let bed of (beds | orderBy: order)" (dragstart)="onDrag($event, patient)">

I understand that impure pipes update very often. What I don't understand is that the order of my array changes even though no data has changed. I have 3 properties that the objects can be ordered by, and it only happens with two of them for some reason, and it's the same 3 objects that are constantly changing positions.

I know it's probably not easy to figure out what is wrong with the information given, but I was hoping someone could have a clue. If it's any help, the three objects that are changing positions have the same value in the property that the array is being ordered by, although there are other objects with that value too that are not moving.

0

There are 0 answers