Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sort an array twice?

 
vanessa english
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10109
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 48404
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10109
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the Japanese still use that numbering system.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic