Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

NEWBIE: How to re-sort two-dimensional int array?

Posted on 2006-07-06
13
Medium Priority
?
267 Views
Last Modified: 2010-03-31
Dear Experts,

Another lazy question...

I have this two-dimensional int array, sorted by the first index:

int[][] myArray = new int[4][2];

With these values:

1  745
2  123
3  999
4  876

What's the easiest (laziest) way to re-sort it by the second index, so I get this:

2  123
1  745
4  876
3  999

Thanks!
BrianMc1958

0
Comment
Question by:BrianMc1958
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 17053550
Easiest way is probably to use a variation of what you used to sort by the "first index". Since there are dozens of ways to do these kinds of things, it would probably be best if you explained and/or posted code of your existing sort.
0
 

Author Comment

by:BrianMc1958
ID: 17053655
I'm reading data from a text file, say of customer information.  The first index in my array is simply an increment (1, 2, 3...).  The second index holds a value (say, customer age.)  I want to retain the original values in the original order, but also have a secondary, sorted array, so I can read through the result sorted by customer age.

In this case, quick-and-dirty is fine.  It's just an in-house thing, sorting just a couple dozen fields...
0
 

Author Comment

by:BrianMc1958
ID: 17053676
So to answer your question, I'm not sorting the first time.  I'm just incrementing.  Now I have to sort.
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 13

Accepted Solution

by:
Webstorm earned 1000 total points
ID: 17053685
Hi BrianMc1958,

You can use :

    java.util.Arrays.sort(myArray,
         new java.util.Comparator(){
              public int compare(Object o1,Object o2)
              { return ((int[])o1)[1]-((int[])o2)[1];  }});
0
 

Author Comment

by:BrianMc1958
ID: 17053746
To Webstorm:  I'm trying it, but getting funny results...  
0
 

Author Comment

by:BrianMc1958
ID: 17053785
It looks (vaguely) like it's subtracting one from my first index, and not actually sorting...
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 17053820
it's the comparator : comparing two value o1 and o2
each are int[], comparing  o1[1] with o2[1]
Comparator.compare must return an integer value :
   <0   when first < second
   =0   when first = second    
   >0   when first > second    
-> same as first - second

0
 

Author Comment

by:BrianMc1958
ID: 17053825
Whoops!  Sorry.  You were right.  (I had swapped my indices at one point...)

Thanks a lot!
--BrianMc1958
0
 
LVL 35

Expert Comment

by:girionis
ID: 17053830
Try this:

int[][] myArray = {{1, 745}, {2, 123},{3, 999},{4, 876}};
        int temp = 0;
        System.out.println(myArray.length);
        for (int i=0; i<myArray.length; i++)
        {
            temp = myArray[i][1];
            if ((i+1 < myArray.length) && (myArray[i][1] > myArray[i+1][1]))
            {
                myArray[i][1] = myArray[i+1][1];
                myArray[i+1][1] = temp;
            }
           
        }

Not the best solution but it works (I hope)
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 17053837
>> I'm trying it, but getting funny results...  
what are thoses funny results ?
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 17053851
:-)
0
 

Author Comment

by:BrianMc1958
ID: 17053858
Our messages crossed.  (Also, girionis posted in the mean time...)  

I was just mistaken.  It's working correctly now except for one row, which I'm looking into.  I'll post a follow-up if I have to.  Thanks again.  
0
 
LVL 35

Expert Comment

by:girionis
ID: 17053889
My code will work for your array, if you run it with more values it will probably fail. Better to follow Webstorm's suggestion or try to hack my example.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question