aspose file tools*
The moose likes Beginning Java and the fly likes Sort an array twice? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sort an array twice?" Watch "Sort an array twice?" New topic
Author

Sort an array twice?

vanessa english
Greenhorn

Joined: Nov 10, 2012
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

Joined: Jan 03, 2004
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

Joined: Mar 17, 2011
Posts: 8427
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40059
    
  28
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

Joined: Jan 03, 2004
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

Joined: Oct 13, 2005
Posts: 40059
    
  28
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

Joined: Mar 17, 2011
Posts: 8427
    
  23

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

Joined: Oct 13, 2005
Posts: 40059
    
  28
I think the Japanese still use that numbering system.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
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."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sort an array twice?