push() pushes to the back of an array. What possible solutions are there for pushing either to the front of an array, or to a specified index in an array?


Nothing like that exists because the obvious solution would involve considerable, variable gas cost to reorganize data at rest.

There are patterns that combine mappings and arrays to create iteratable sets with random access. If it is to be sorted or organized in any particular order then one needs to code for that as well.

Have a look over here for some ideas Are there well-solved and simple storage patterns for Solidity?

Hope it helps


Just to add to Rob's answer, I have found a library for a linked list in Solidity, though it comes with the disclaimer that I have not audited the code, nor have I yet had the opportunity to check how gassy it is: https://vittominacori.github.io/solidity-linked-list/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.