acekizzy
acekizzy

Reputation: 67

*ngFor force prints in ascending order

I have an object that I am trying to display in markup. But *ng keeps printing it in ascending order. Desired outcome is for elements to be printed in the order that they are. This is my stackblitz code https://stackblitz.com/edit/angular-5nr2uk

Upvotes: 1

Views: 256

Answers (2)

user10858335
user10858335

Reputation: 1

As the description in official API doc:

The output array will be ordered by keys. By default the comparator will be by Unicode point value.

This pipe also provides an optional function parameter to re-define sort order:

{{ input_expression | keyvalue [ : compareFn ] }}
(a: KeyValue, b: KeyValue) => number

More detail here

Upvotes: -1

user4676340
user4676340

Reputation:

As per the documentation, you can provide a custom compare function to the pipe.

Simply provide a function that returns zero everytime.

customCompare(a, b) {
  return 0;
}
*ngFor="let key2 of key.value | keyvalue:customCompare;"

Working stackblitz

Upvotes: 3

Related Questions