A place to log my programming solutions.

Tagged :: technique

9
Aug 09

Simple List Reordering Technique

At some point in our programming careers, we’ve had to reorder server-side (probably database driven) lists. For example, allowing users to set the specific sequence that image thumbnails are displayed on the front-end of a photo album.

For “move-item-forwards” functionality, the logic may look like:

move_item_up( index ){
  Store the element at index 3 to a temporary variable.
  Set the value of index 3 to the value of index 4.
  Set the value of index 4 to the temporary variable's value.
}

The “technique” comes into play now, when you have to do the “move-item-backwards” functionality.

Instead of:

move_item_back( 4 );

Simply use:

move_item_up( 3 );

Moving an item backwards in a list is the same as moving the previous item forwards. This technique is rather simple and perhaps painfully obvious to some, though it may be useful to others.

About NVNCBL and Myself

Contact Me