0

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

5
  • 1
    Have you tried anything? Commented Nov 29, 2015 at 11:16
  • yes, if you want I can show you my try, but here I described problem partly, the actual problem is slightly different so the code Commented Nov 29, 2015 at 11:18
  • Every sorting algorithm would work as soon as you provide the right compare function. Commented Nov 29, 2015 at 11:28
  • @vishalgajera thank you for the answer Commented Nov 29, 2015 at 12:14
  • @Humoyun if you are satisfied with my answer, please up-vote Commented Nov 30, 2015 at 10:46

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

it is right, but I should also keep track of index not just values, main problem I am facing is after sorting I lose initial positions of indexes as they are sorted
0

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;
    }
}

3 Comments

"Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime." You just gave that man a fish.
@JérômeBrunel what you mean ?
You threw the solution at him without giving any explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.