Solved

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

Posted on 2006-07-06
13
255 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
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 16

Expert Comment

by:imladris
Comment Utility
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
Comment Utility
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
Comment Utility
So to answer your question, I'm not sorting the first time.  I'm just incrementing.  Now I have to sort.
0
 
LVL 13

Accepted Solution

by:
Webstorm earned 250 total points
Comment Utility
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
Comment Utility
To Webstorm:  I'm trying it, but getting funny results...  
0
 

Author Comment

by:BrianMc1958
Comment Utility
It looks (vaguely) like it's subtracting one from my first index, and not actually sorting...
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 13

Expert Comment

by:Webstorm
Comment Utility
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
Comment Utility
Whoops!  Sorry.  You were right.  (I had swapped my indices at one point...)

Thanks a lot!
--BrianMc1958
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
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
Comment Utility
>> I'm trying it, but getting funny results...  
what are thoses funny results ?
0
 
LVL 13

Expert Comment

by:Webstorm
Comment Utility
:-)
0
 

Author Comment

by:BrianMc1958
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now