I have a comma delimited string held within a database field that could contain any number of values:
23,45,21,40,67,22
I need to be able to somehow switch two values, so for example I know I need to move 45 one position down the string, so I end up with:
23,21,45,40,67,22
The reason for this is that the numbers all correspond to the IDs held in another database table, and their position in the sting determine the order those items will be printed on screen. Before you ask about database design - I've inherited it and it cannot be changed without significant work to an entire application.
So I've thought about exploding the string, identifying the position of the target number and swapping it with the one next-door, but I'm unsure of how this can be achieved when the total number of values is not known.
Any things? I suspect the solution will be cumbersome, but needs must!!
assuming you need to only move the desired value down one position in the array: