Humoyun Ahmad
Humoyun Ahmad

Reputation: 3081

How to sort 2D array in Java based on two column's value

I have a problem where I should sort out array of arrays and get sorted indexes of the array, I think some example will demonstrate my problem better than just describing by words. So, I present several examples:

1-example:
n=3 
[1, 4] row=0
[2, 5]
[3, 6] row=2
output should be : 0 1 2 (explanation is below)
2-example:
n=5
[8, 9]  row=0
[4, 6]  row=1
[5, 11] row=2
[3, 4]  row=3
[4, 7]  row=4
[2, 6]  row=5
output should be : 3 5 1 4 0 2(explanation is below)

Sorting criteria mainly based on second column's value, first I should print the second column's smallest value's index, in 1-example it is 4 and it's index is 0 . If we encounter same values in second column as in 2-example (1 and 5 rows are same) then we should compare first columns corresponding values and print the smaller one's index first. Another more precise example of the problem:

n=3
[4, 6]  row=0
[1, 6]  row=1
[2, 6]  row=2
output should be : 1 2 0 

EDIT: There is always 2 columns and n rows

Upvotes: 0

Views: 1364

Answers (2)

Vishal Gajera
Vishal Gajera

Reputation: 4207

Here is you complete solution try this,

public class TwoDimensitnArraySort {
public static void main(String[] args) {
    int ary[][] = {{8, 9},{4, 6},{5, 11},{3, 4},{4, 7},{2, 6}};

    ArrayList<TwoDArray> list = new ArrayList<TwoDArray>();

    for(int i = 0;i<ary.length;i++){
        int k = ary[i][0];
        int v = ary[i][1];
        list.add(new TwoDArray(k, v));
    }

    Collections.sort(list);
    int index = 0;
    for(TwoDArray element : list){
        for(int i = 0;i<ary.length;i++){
            if(element.getKey() == ary[i][0] && element.getValue() == ary[i][1]){
                System.out.print(i + " ");
            }
        }
    }
}
}

class TwoDArray implements Comparable<TwoDArray>{
    int key;
    int value;

    public TwoDArray(int key,int value) {
        this.key = key;
        this.value = value;
    }


    public int getKey() {
        return key;
    }



    public void setKey(int key) {
        this.key = key;
    }



    public int getValue() {
        return value;
    }



    public void setValue(int value) {
        this.value = value;
    }



    public int compareTo(TwoDArray o) {
        if(o.getValue() >= this.getValue()){
            return -1;
        }else if (o.getValue() < this.getValue()){
            return 1;
        }
        if(o.getValue() == this.getValue()){
            if(o.getKey() >= this.getKey()){
                return -1;
            }else if (o.getKey() < this.getKey()){
                return 1;
            }
        }

        return 0;
    };
    @Override
    public String toString() {
        return this.key + ":" + this.value;
    }
}

Upvotes: 0

jeerbl
jeerbl

Reputation: 7867

Basically, for this question, I think any sorting algorithm would work. You just need to specify your compare function to compare two elements.

For example, if you want to Bubble sort, in your case, with this algorithm (pseudocode taken from Wikipedia):

procedure bubbleSort( A : list of sortable items )
  n = length(A)
  repeat 
    swapped = false
    for i = 1 to n-1 inclusive do
      if A[i-1] > A[i] then /* COMPARE LINE */
        swap( A[i-1], A[i] )
        swapped = true
      end if
    end for
    until not swapped
end procedure

You just need to replace the comparison on the line commented with COMPARE LINE with a compare function that would compare your objects just like you need (based on the second element and, if equal, the first element).

For example, replace this line by if compare( A[i-1], A[i] ) then.

To summarize, every sorting algorithm would work, as long as you provide the right compare function.

Upvotes: 1

Related Questions