Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

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

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
BrianMc1958
Asked:
BrianMc1958
  • 6
  • 4
  • 2
  • +1
1 Solution
 
imladrisCommented:
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
 
BrianMc1958Author Commented:
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
 
BrianMc1958Author Commented:
So to answer your question, I'm not sorting the first time.  I'm just incrementing.  Now I have to sort.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
WebstormCommented:
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
 
BrianMc1958Author Commented:
To Webstorm:  I'm trying it, but getting funny results...  
0
 
BrianMc1958Author Commented:
It looks (vaguely) like it's subtracting one from my first index, and not actually sorting...
0
 
WebstormCommented:
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
 
BrianMc1958Author Commented:
Whoops!  Sorry.  You were right.  (I had swapped my indices at one point...)

Thanks a lot!
--BrianMc1958
0
 
girionisCommented:
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
 
WebstormCommented:
>> I'm trying it, but getting funny results...  
what are thoses funny results ?
0
 
WebstormCommented:
:-)
0
 
BrianMc1958Author Commented:
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
 
girionisCommented:
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

Industry Leaders: 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!

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now