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.

Entity sorting after updates is not stable

See original GitHub issue

Per https://www.reddit.com/r/reduxjs/comments/ud58hg/sortcomparer_in_rtk_switches_the_ordering_of/ :

I’m using createEntityAdapter with sortComparer to sort some entities based on a timestamp for when they were last updated. If the entities haven’t been updated yet, this timestamp is null. There are many cases of these null timestamps in my data.

I’m displaying these sorted entities in a table, where I can also make edits that will modify the entity object by dispatching an action, which uses the updateOne entity adapter method. Note that this edit has no impact on the timestamp used for sorting.

My problem is: if I edit an entity which has a matching timestamp as other entities (this happens often in the case of null timestamps), that item will suddenly be re-ordered to appear at the bottom of my table.

It seems like this happens because the sortComparer function returns 0 since the comparison is equal, so the order is then based on the ordering of entities in my slice. But because the entity was updated, redux re-orders the entity in the slice to now be at the bottom (which I can see happening using the redux dev tools). This means that any time I change an entity that has an equal sort comparison, it will now be automatically reordered to be last.

Is there any way around this? It seems crazy to me that I can’t rely on a consistent ordering for items that have an equal comparison.

My analysis:

Hmm. We ultimately just call array.sort() with whatever sorting comparator you provided.

However, given that it’s doing Object.values(entities), the order of items in that array is going to be based on insertion order into that object, I think. Given that the update logic is implemented by deleting the existing item ID from state.entities before it gets re-added later, I can see how that might result in an updated item technically getting added later.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
markeriksoncommented, Jun 29, 2022

This should actually be fixed now in #2464 and will be out in whatever the next patch or minor release is.

0reactions
Reisheicommented, Dec 5, 2022

@markerikson : i’m using version 1.8.1. Maybe i need to upgrade. Thanks for the info.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity queries with sorting and ordering are not working in a ...
Problem/Motivation. 1. Install latest HEAD (8.7.x) with minimal profile. 2. Enable workspaces, toolbar modules 3. Visit admin/content
Read more >
Sorting not working when update entity - Hibernate Search
Hi, I am using hibernate search 5.11 , i have added base entity below @CreationTimestamp @Column(updatable = false, name = “created_date”) @Field(analyze ...
Read more >
Hibernate - Ordering of update queries fired - Stack Overflow
Take a look at this answer. Basically, the strict order of updates is not defined in Hibernate. It happens that the current implementation ......
Read more >
sorting is wrong with Entity Framework for oracle
Hi, I've downloaded the Entity Framework for Oracle beta and I found a bug. It's easy to repro: - Create a model with...
Read more >
Tutorial: Add sorting, filtering, and paging with the Entity ...
In this tutorial you add sorting, filtering, and paging functionality ... do not change when you change the code later to use a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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