This is a bubble sort:

...

Thanks willakawill. I didn't have the time to post code the other day. One thing, it's a bit more efficient if you limit the loops so you only go through the entries that are actually necessary.

Um... it's been a while since I played with this, but from memory, I used to keep track of the last swap performed, then end the inner loop at that point, next time around.

Since you've pushed the values as far as you can in one direction, there's no need to go beyond that point in future iterations, as you know there's nothing there to swap.

In fact, I used to code a bi-directional variant. It simply "bubbles" through in one direction, then the other, then back the other way again. That way, you can set both upper and lower limits, and cut down the looping much more quickly.

Of course, you can just find some sort of callable class, or sample code, for a faster sort I suppose. Bubble is one of the slowest available. Not

*the* slowest, though. A year or two back I came across a website which demonstrated a bunch of different sort routines. One of them, I forget what they called it, but it effectively performed random swaps until it found the items in sequence. The amount of time it would take to sort a given number of items could range from the shortest theoretically possible time for any sort, to infinity :).