ngrx / platform

Reactive State for Angular
https://ngrx.io
Other
8.01k stars 1.97k forks source link

Sort on Entity Adapter Collection Methods not working #2778

Closed raphibolliger closed 3 years ago

raphibolliger commented 3 years ago

Minimal reproduction of the bug/regression with instructions:

Use the following StackBlitz example to create a reproduction: https://ngrxsort.stackblitz.io

You can try it and add different Todo's. They should be sorted in alphabetic order but they will be only sorted after a full page refresh or by clicking the refresh button which calls setAll() in the Entity Adapter.

Reducer implementation: https://stackblitz.com/edit/ngrxsort?file=src%2Fapp%2Fstore%2Fapp.reducers.ts

NgrxSortProblem

Expected behavior:

With a complete setup of NgRx (Store, Entity and Effects) I add new objects with the help of Entity Adapter. They should be sorted after the add or update operation. The sort comparer ist defined in the reducer. It will only sort after a complete reload and call to the settAll() method. Maybe it's a bug or just a wrong handling of mine.

Versions of NgRx, Angular, Node, affected browser(s) and operating system(s):

NgRx: 10.0.1 Angular: 10.0.9

timdeschryver commented 3 years ago

This is the intended behavior, using state.entities is a dictionary and doesn't sort the items. state.ids however sorts the entities in the order described by the sortComparer.

I would recommend to use the entity selectors. See the refactored example at https://stackblitz.com/edit/ngrxsort-97wggf?file=src%2Fapp%2Fstore%2Fapp.reducers.ts.