0

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.

void DoubleArraySort()
{
    int k,m,Hide;

    boolean DidISwap;

    DidISwap = true;

    while (DidISwap)
    {
        DidISwap = false;
        for ( k = 0; k < Row - 1; k++)
        {
            if ( Numbers[k][0] > Numbers[k+1][0] )
            {
                for ( m = 0; m < Col; m++)
                {
                    Hide = Numbers[k ][m];
                    Numbers[k ][m] = Numbers[k+1][m];
                    Numbers[k+1][m] = Hide ;
                    DidISwap = true;
                }
            }
        }
    }
}
0

3 Answers 3

2

Use an object ZipCode like this:

public class ZipCode{

    private String zipcode;
    private ArrayList<String> adds

    public ZipCode(String zip){
        zipcode = zip;
        adds = new ArrayList<String>();
    }

    public void addAddress(String address){
        adds.add(address);
        Collections.sort(adds);
    }

}

Keep an array of ZipCodes sorting them necessarily:

ZipCode[] zips = . . . 
  .
  .
  .
Arrays.sort(zips);
Sign up to request clarification or add additional context in comments.

Comments

0

First of all, are you aware that Java provides a more efficient sorting mechanism out of the box? Check the Arrays class.

Secondly you have to be very careful with your approach. What you are doing here is swapping all the elements of one row with the other. But you are not doing the same thing within each row. So you need a separate nested loop outside the current while (before or after, doesn't make a difference), which checks the houses themselves and sorts them:

   for ( k = 0; k < Row; k++)
   {
     do
     {
       DidISwap = false;

       for ( m = 0; m < Col-1; m++)
       {
         if (Numbers[k][m] > Numbers[k][m+1])
         {
           Hide = Numbers[k][m];
           Numbers[k][m] = Numbers[k][m+1];
           Numbers[k][m+1] = Hide;
           DidISwap = true;
         }
       }
     }
     while (DidISwap);
   } 

However, your approach is very inefficient. Why don't you put the list of houses in a SortedSet, and then create a SortedMap which maps from your postcodes to your Sorted Sets of houses? Everything will be sorted automatically and much more efficiently.

You can use the TreeMap for your SortedMap implementation and the TreeSet for your SortedSet implementation.

Comments

0

I / we could try to tell you how to fix (sort of) your code to do what you want, but it would be counter-productive. Instead, I'm going to explain "the Java way" of doing these things, which (if you follow it) will make you more productive, and make your code more maintainable.

  1. Follow the Java style conventions. In particular, the identifier conventions. Method names and variable names should always start with a lower case character. (And try to use class, method and variable names that hint as to the meaning of the class/method/variable.)

  2. Learn the Java APIs and use existing standard library classes and methods in preference to reinventing the wheel. For instance:

    • The Arrays and Collections classes have standard methods for sorting arrays and collections.

    • There are collection types that implement sets and mappings and the like that can take care of "boring" things like keeping elements in order.

  3. If you have a complicated data structure, build it out of existing collection types and custom classes. Don't try and represent it as arrays of numbers. Successful Java programmers use high-level design and implementation abstractions. Your approach is like trying to build a multi-storey car-park from hand-made bricks.


My advice would be to get a text book on object-oriented programming (in Java) and get your head around the right way to design and write Java programs. Investing the effort now will make you more productive.

4 Comments

That is a comment, not an answer.
@alphazero - No. It is an answer. It is instructing the OP in the best way to solve his problem.
It is like a patient going to a doctor with an illness and getting a lecture on good hygiene instead of a prescription. Good advice, of course, instructive certainly, but that does not answer his question.
No. It is like a patient with gangrene going to a doctor and asking for a pain killer. Answering his question won't help him. This is not "good hygiene" stuff. It is much more fundamental than that. Anyway, you won't convince me that this is an inappropriate answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.