User:Andy Maloney/Notebook/Lab Notebook of Andy Maloney/2011/04/21/Learning how to sort in Java

Sorting with names
So I had a difficult time understanding how to sort things yesterday so I decided to run a few simple examples. The first code I'm going to work on is sorting names. I will use the names of people in the lab and sort them. The output of this gives

So as expected, the list is in alphabetical order. Now, I want to include all names with out capitalization to see what happens. As expected, this does not put the names in what a human would call alphabetical order. It does put the names in ASCII order.

While this is fine and dandy for a computer, it does me absolutely no good. This is one of those unfortunate circumstances that when people where first making ASCII codes for an alphabet, they didn't put them in a sortable order that a human would have done, i.e. Steve should come after andy. This is now a legacy issue because I'm sure no one will want to change the ASCII dictionary of letters such that they correspond to a human's way of sorting and I'm sure it would be a nightmare to do so. Oh well, I guess it's up to people who program to make algorithms to sort things the way you were taught in grade school.

Thankfully, there are some workarounds to the above sorting issue. I'm even more thankful that there is a simple one line workaround. To order names with capital and non capital letters, one just adds java.text.Collator.getInstance to the Arrays.sort function. The output of this is as expected

Sorting with numbers
The next step is to try and see how sorting works with numbers. I just added a list of numbers in the above code. The output is the following.

This is good. It would appear that Arrays.sort will sort numbers as one would expect them to be sorted even though the ASCII sorting would put 400 next to 48. The next step is to see if I can make a string of numbers and sort them, (i.e. 01 and 0001). This is nice, it would appear that Arrays.sort does sort things as I would expect them to be sorted even with numbers as strings.

Sorting file names
Okay, it would appear that the sorting is working out just fine. The problem is that I have a bunch of file names that I want to sort. The file names look like Cam6-0001.jpg, Cam6-0002.jpg,...Cam6-42300.jpg. Let's see if Arrays.sort will work with sorting these types of strings. Okay, the output is exactly what I want.

But this is not what happened yesterday when I was trying to order the file names.

Reexamining yesterday's code
So, I am not getting the files in lexicographical order with the above code. I have a list that goes from Cam6-1000.jpg to Cam6-10000.jpg. Changing the Arrays.sort to include the Collator.getInstance does not help either.

Okay, so from what I understand, I now need to a function that compares the list values and arranges them in order dependent on their comparisons.

Hmm, I can't seem to get it...

I also looked at the sorting code found at the top of the page and changed it some what. The output shows that this sorting isn't working.

Now I'm really confused.

Andy Maloney 16:49, 21 April 2011 (EDT): I got it. Use someone else's code to do it. Found from Dave Koelle's blog. While this does great for inputing a list in the code, I can't get it to work when it reads file names. So, once again I've hit a road block.

Well, I couldn't get Koelle's code to work. But I did find more from Tim Fennell's Blog and I implemented it below. And this actually works with the simple example I use in array D. The problem is that I can't seem to get it to work with the file names. This is curious.