keyvalue pipe doesn't preserve a Map's sorted order
See original GitHub issueπ bug report
Affected Package
The issue is caused by package @angular/commonIs this a regression?
NoDescription
One of the reasons a developer uses aMap
over an Object
is to have Object-like features with sorted keys. When passing a Map
to *ngFor
with the keyvalue
pipe, the map's sorted order is lost.The bug is that the keyvalue
pipe uses the defaultComparator
for Maps. No sorting should be applied when keyvalue
is passed a Map
.
π¬ Minimal Reproduction
https://stackblitz.com/edit/angular-keyvalue-pipe-map-order?file=src/app/app.component.tsπ Your Environment
Angular Version:
8.0.2
Issue Analytics
- State:
- Created 4 years ago
- Reactions:58
- Comments:33 (17 by maintainers)
Top Results From Across the Web
angular keyvalue pipe sort properties / iterate in order
According to the Angular documentation, the keyvalue pipe sorts the items by key order by default. You can provide a comparer function toΒ ......
Read more >angular keyvalue pipe sort properties / iterate in order-angular.js
Yes, Object properties iterates randomly since it doesn't get stored as array in memory. However you can sort by properties. If you want...
Read more >Loop Object Key Values In Angular Using *NgFor & Angular ...
This keyvalue pipe converts Object or Map into an array of key value pairs. Converted array will be sorted by keys according to...
Read more >Angular keyvalue Sorting & Variation of *ngFor directive
According to the Angular documentation, the keyvalue pipe sorts the items by key order by default. You can provide a compare function to...
Read more >Collections - Laravel - The PHP Framework For Web Artisans
Available Methods; Higher Order Messages; Lazy Collections ... you to chain its methods to perform fluent mapping and reducing of the underlying array....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@ravishivt Agree with you.
Although I donβt think itβs a bug if theyβve intended it that way, there should be an easy way to preserve the order. Why not leave it as is, then we can sort however we want right? Why apply the defaultComparator instead?
Say, my Map doesnβt follow any sorting algorithm, and I want to keep the structure. Thereβs not a way to revert back to that unsorted structure even with a custom comparatorFn
EDIT:
Actually I found a simple way to preserve the order
return 1
because a is the element that gets passed in first, and 1 means it should come first in the array@radotzki Have you heard about map data structure before? Do not disgrace, close the ticket