Fix bug in sort where numbers could get too long

Fall back to strings. Also, add some unit tests.