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?
Asked
Active
Viewed 541 times
1
Rob Hitchens
- 55,151
- 11
- 89
- 145
The Renaissance
- 2,749
- 1
- 17
- 45
2 Answers
2
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
Rob Hitchens
- 55,151
- 11
- 89
- 145
0
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/
The Renaissance
- 2,749
- 1
- 17
- 45