0

It's probably a rather basic question but I can't seem to find an answer that I require online.

However, what's the best way to sort a 2 dimensional array?

I have a pre-populated 2 dimensional array 'mainArr[][]' that contains data:

John Doe - 678DGHJ,Sport
Lisa Parker - 432KH3,Car
John Doe - 678DGHJ, Drive
Peter Bear 4HJ4K3,Bus
John Doe - 4HJK4,Loose

The comma being the separator between the dimension in the array. How can I sort it first by the column first in the array, then by the value in the second part of the array.

The sorted data above would become:

John Doe - 4HJK4,Loose
John Doe - 678DGHJ,Drive
John Doe - 678DGHJ,Sport
Lisa Parker - 432KH3,Car
Peter Bear 4HJ4K3,Bus

So the sorted data in the array of

mainArr[0][0] 

would equal

mainArr[John Doe - 4HJK4][Loose]
7
  • I'm not clear, is the list you have mentioned is the intended outcome or the one you have currently got? Commented Jan 5, 2019 at 5:14
  • The first is the data that I have, and the list below it is the outcome that I want. Commented Jan 5, 2019 at 5:17
  • 4
    You should rethink your design. Why are you storing things in a 2D array when you can create an object with appropriate fields that can be accessed and sorted with Collections.sort() with very little additional programming on your part? Commented Jan 5, 2019 at 5:19
  • 2
    looks like a homework question. Commented Jan 5, 2019 at 5:30
  • 2
    The tail shouldn't wag the dog... can you explain the rationale behind this 2D array approach? What I'm getting at is that you might think that you're wedded to this terribad approach when you could very well be able to free yourself. The alternative is to do your own implementation of a merge sort and resolve equal strings with the contents of the second array (you'd only ever care about the second array in the case of a tie). The trick is to retain the index of the value being evaluated so that it can be used to retrieve the corresponding second value if needed. Commented Jan 5, 2019 at 5:36

1 Answer 1

2

You can use the Comparator like below to sort your array,

import java.util.Arrays;
import java.util.Comparator;

public class Main {
    public static void main(String args[]) {
        String[][] mainArr = new String[][] {{"John Doe - 678DGHJ", "Sport"}, {"Lisa Parker - 432KH3", "Car"}, {"John Doe - 678DGHJ", "Drive"}, {"Peter Bear 4HJ4K3", "Bus"}, {"John Doe - 4HJK4", "Loose"}};

        Arrays.sort(mainArr, new Comparator<String[]>() {
            @Override
            public int compare(String[] entry1, String[] entry2) {
                if (entry1[0].equals(entry2[0])) {
                    return entry1[1].compareTo(entry2[1]);
                }
                return entry1[0].compareTo(entry2[0]);
            }
        });

        for (int i = 0; i < mainArr.length; i++) {
            System.out.println(mainArr[i][0] + ", " + mainArr[i][1]);
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

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.