Fix loop condition in python sort driver.
Add a lua version of the sort code.