question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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/common

Is this a regression?

No

Description

One of the reasons a developer uses a Map 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:closed
  • Created 4 years ago
  • Reactions:58
  • Comments:33 (17 by maintainers)

github_iconTop GitHub Comments

52reactions
Drageauxcommented, Jul 9, 2019

@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

// *.component.ts
asIsOrder(a, b) {
  return 1;
}
<!-- *.component.html -->
<div
      *ngFor="
        let someVal of someMap | keyvalue: asIsOrder;
        let i = index
      ">
</div>

return 1 because a is the element that gets passed in first, and 1 means it should come first in the array

40reactions
djleonskennedycommented, May 27, 2020

@radotzki Have you heard about map data structure before? Do not disgrace, close the ticket

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found