Ordering cache data

Hi,
I have a question concerning the way the cache stores updated data, particularly the order in which it stores it.

Let’s say I have an initial query that retrieves data which is sorted in a particular way from the backend. That data is fetched in the correct order and stored in the cache following the same sorting logic indicated in the backend.

However, the behaviour I’m observing when I perform an update mutation on that data is that first the data is updated correctly in the cache but then the order of the data is also modified, with the newly modified element going to the end of the list.

I’ve tried sorting the obtained data once it’s received in the front end and that works initially but again once it is updated, the order is changed.

My question is what is the best way to deal with sorting data when dealing with the cache?

I am using react, so I was wondering maybe I should store the data in a state variable instead of directly displaying the query results and then perform the sorting logic on that state object.

Thank you and I would love to hear what “architecture” you guys are using when it comes to data that needs to be sorted a certain way.

PS: Here’s a stackoverflow version of this question where you can take a look at my code: