0

So i have something like this

011 - 50
111 - 75
001 - 25

This is my program :

public class Urut {
public void Sort (int [][] input1,int[] input2){
 for (int i = 0; i < 5; i++){
    for (int j = 0; j < 10; j++){
            for (int k = 0; k < 10-1; k++){
              if (input2[k] > input2[k+1]){
            int t;
                t = input1[k][i]; 
                input1[k][i] = input1[k+1][i]; 
                input1[k+1][i] = t; 
            }
        }
    }
 }
   for (int i = 0; i < 10; i++){
    for (int j = 0; j < 10-1; j++){
        if (input2[j] > input2[j+1]){
                int te = input2[j];
                input2[j] = input2[j+1];
                input2[j+1] = te;
        }
        }
    }
    for (int i = 0; i < 10; i++){
        for (int j = 0; j< 5; j++){
            System.out.print(input1[i][j]);
        }
        System.out.println(" " + input2[i]);
    }
}

}

I want to sort the 2D array (the 0 and 1) based on the value on the right (which is a 1D array). Can someone help me?

1 Answers1

0

Let me guess!

It is not a 2D array but it is a map. Ok, 2D array with two rows. First row is like this:

011, 111, 001

and second is like this

50, 75, 25

You want to sort the first row, considering second row. So for your example, it will be:

001, 011, 111 because of 25, 50, 75

If I am right, an answer is here: How to sort a Map on the values in Java?

If I am wrong, please add a comment, I will remove this answer.


EDIT :

Java: Sort an array

Community
  • 1
  • 1
ruhungry
  • 4,116
  • 18
  • 51
  • 93
  • no, it's 2 different arrays. the first one goes like this first row 011 second row 111 third row 001 i want to sort them based on the one other array which goes like this first row 50 second row 75 third row 25 i want to make it something like this 001 - 25 011 - 50 111 - 75 – user3519493 Apr 10 '14 at 12:53
  • ok. my hint for you is: put more code, explain your problem deeper – ruhungry Apr 10 '14 at 12:55
  • So, you have got 2 arrays. Sort 1st one, sort 2nd one. Done – ruhungry Apr 10 '14 at 12:58
  • i can't. that's my problem over there. my friend suggest me to use comparable or comparator but i just can't understand it for my problem – user3519493 Apr 10 '14 at 13:02
  • paste your code. **EDIT:** I edited my post, take a look – ruhungry Apr 10 '14 at 13:03