How can I sort a table using a for loop?