Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Sort an array twice?

vanessa english
Greenhorn
Posts: 1
I am working on a program that displays zip codes and house numbers. I need to sort the zip codes in ascending order in the first column then sort the house numbers from left to right, keeping them with the same zip code.
For instance:

Looks like this:
90153 9810 6037 8761 1126 9792 4070

90361 2274 6800 2196 3158 9614 9086

I want it to look like this:

90153 1126 4070 6037 8761 9792 9810

90361 2186 2274 3158 6800 9086 9614

I used the following code to sort the zip codes but how do I sort the house numbers? Do I need to add a loop to sort the numbers to this code? If so, where? So sorry I couldn't make the code indent correctly

Jeff Verdegan
Bartender
Posts: 6109
6
vanessa english wrote:
I used the following code to sort the zip codes but how do I sort the house numbers?

In a single loop, where you do the comparisons, your logic should look like this:

Winston Gutkowski
Bartender
Posts: 10527
64
vanessa english wrote:I am working on a program that displays zip codes and house numbers. I need to sort the zip codes in ascending order in the first column then sort the house numbers from left to right, keeping them with the same zip code.

Is this an exercise in writing sorts, or are you allowed to use existing sort functions? Because if the latter, it seems to me that you could do the whole thing with the sort methods provided by java.util.Arrays; specifically: this one and this one.

"2-Dimensional" arrays are implemented in Java as an array of arrays; and arrays are objects.
Specifically, an int[][] is an array of int[]s; thus, all you really need to do is create a Comparator that orders an int[] by its first element (the zip code), and then sort the subsequent elements of each int[] "line" using the first method I listed above.

HIH

Winston

Campbell Ritchie
Sheriff
Posts: 50687
83
• 1
Why do you not have an Address object which can be sorted with two comparators? That will obviate the array of arrays; a simple array is a much more elegant solution.

Jeff Verdegan
Bartender
Posts: 6109
6
Campbell Ritchie wrote:Why do you not have an Address object which can be sorted with two comparators? That will obviate the array of arrays; a simple array is a much more elegant solution.

@OP: An Address object is definitely preferable to parallel arrays. I assumed that's what you already had. However, the "two comparators" approach is for a different problem than what I think you're trying to solve. If you want to sometimes sort by zip and other times sort by house number, then you can use two comparators.

However, if you want to sort by zip and just use house number for the "tie breaker" (which is what I think you're trying to do), then you do like I suggested in my previous post. If you're using the sort() methods provided in the core API, you'd only use a single comparator in that case, or else have the class implement Comparable.

Campbell Ritchie
Sheriff
Posts: 50687
83
You can create a compareTo method (Comparable<T> interface) which returns the difference between zips, or the difference between house numbers if the zips are the same. That would make your address class easily sortable.

Beware: House numbers are not numbers. If house numbers were numbers then no 1 would be 1 away from no 2 and no 100 would be 2 away from no 98. In this country you can get peculiar house numbers. I have had friends living at No 11a, 36a and 2½!

Winston Gutkowski
Bartender
Posts: 10527
64
Campbell Ritchie wrote:Beware: House numbers are not numbers. If house numbers were numbers then no 1 would be 1 away from no 2 and no 100 would be 2 away from no 98. In this country you can get peculiar house numbers. I have had friends living at No 11a, 36a and 2½!

Not as bad as Japan where, from what I understand, houses used to be numbered in the order they were built. Don't know if they've changed it now, but apparently it made finding an address an absolute nightmare, even for their own postal service.

Winston

Campbell Ritchie
Sheriff
Posts: 50687
83
I think the Japanese still use that numbering system.

Jeff Verdegan
Bartender
Posts: 6109
6
Winston Gutkowski wrote:Not as bad as Japan where, from what I understand, houses used to be numbered in the order they were built. Don't know if they've changed it now, but apparently it made finding an address an absolute nightmare, even for their own postal service.

Not only that, but only major thoroughfares are named. Small streets aren't. Addresses are something like "Tokyo, Suginami Ward, District 1, Block 41, Building 2." Streets are not generally laid out in a grid, so you can't just look for increasing/decreasing numbers and a cross street, although at any level, number X is generally next to, or at least near, number X + 1.

I lived right around here for a bout a year, though I don't recall the exact location or address. I once got directions to someone's house that included "turn right at the cigarette machine that says 'Smokin' Clean' on it."