What Should Undo Do After a List Has Moved?

https://hackernoon.imgix.net/images/YMTYCyv7AaXHqNG5Uy16QwYk0eh2-n783dn1.png

Alder, Birch, and Cedar are three cards in a reading list. Delete Birch and an Undo button appears. Before pressing it, move Cedar to the top. Where should Birch go when you bring it back? This tiny sequence exposes a product decision that is easy to bury in an array operation.

Start by recording the index. Birch was at index one. After the move, the list reads Cedar, Alder, so inserting at index one gives Cedar, Birch, Alder. That is a perfectly defensible result if the promise is “restore the second slot.” Someone who remembers placing Birch just after Alder may find it surprising.

Try a different promise. On deletion, remember the IDs of the neighbours along with the deleted card and its ID. On restore, look for the former preceding neighbour first. If Alder survives, place Birch immediately after it. The result in our example is Cedar, Alder, Birch....

Copyright of this story solely belongs to hackernoon.com. To see the full text click HERE

Read more