2

I am working on 2D array and I need to organize my array. Explanation, my array is an Array String and it's containing in the header some double value, and I want to sort the array in function of the header.

The first thing I thought was to get the header and sort the header, get all the columns of the array in another array and comparate the value of the header ordered to each column index[0] and push to another array.

However, I thought there is a way to do it easily, but I don't know if there is, I saw the possibility to sort directly in the array and organize in function of this but I have no idea how to it.

Just in case :

Original array 
String[][]myArray = {{3,Toto,30},{2,Tata,29},{1,Titi,13}};

Array expected 
String[][]newArray = {{1,Titi,13},{2,Tata,29},{3,Toto,30}};

Open to proposal! Thanks.

Edit : The header could be Double value.

2 Answers 2

2

If I understood you correctly, seems like this:

Arrays.stream(myArray)
      .sorted(Comparator.comparingDouble(x -> Double.valueOf(x[0])))
      .toArray(String[][]::new);  

Of course you can do that in place too, via Arrays::sort and that Comparator

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

4 Comments

In you prefer, I want to sort my array from the header. Easy to say ahah. Thanks for your help but it's didn't work I don't know why
@TotoD Eugene gave you the correct answer according to your specification.
If the code snippet provided doesn't work then there should be an Exception thrown or something. Please update your question.
I guess you forgot to assign the result to an instance :) If so maybe next time use Arrays.sort(myArray, Comparator.comparingDouble(x -> Double.valueOf(x[0]))); as sugested by Eugine.
0

Previous answer just returned the original array when I tried it. Here's what worked for me:

String[][] myArray = {{"3","Toto","30"},{"2","Tata","29"},{"1","Titi","13"}};
Arrays.sort(myArray, new Comparator<String[]>() {
    public int compare(String[] lhs, String[] rhs) {
        try {
            double lhs_value = Double.parseDouble(lhs[0]);
            double rhs_value = Double.parseDouble(rhs[0]);
            if (lhs_value < rhs_value) return -1;
            if (lhs_value > rhs_value) return 1;
            return 0; //else the two are equal
        } catch (NumberFormatException e) {
            //handle exception
            return 0;
        } 
    }
});

My output:

1 Titi 13 
2 Tata 29 
3 Toto 30 

Here Arrays.sort (see Javadoc) takes in two parameters: an array you're sorting (myArray), and a Comparator (see Javadoc), which is an interface that allows comparison between two arbitrary types. Since {"3", "Toto", "30"} isn't a type you created, and is just a String[], we're going to make a Comparator<String[]> inline.

Comparators implement a function "compare" which takes in two elements, and returns -1, 0, or 1 to determine the ordering of the elements. Essentially "compare" gets called multiple times in the sorting process to precisely determine sorted order. Here's some pseudocode:

public int compare(Object a, Object b)
    if (a comes "before" b) return -1
    if (a is "equal" to b) return 0
    if (a comes "after" b) return 1

If that isn't clear, you can learn more about the Comparator interface here: https://www.geeksforgeeks.org/comparator-interface-java/

I know I used "before" and "after" earlier, which are a little fuzzy and non-rigorous. If you're more mathematically inclined, here's a good discussion of the Comparator interface: https://math.stackexchange.com/questions/1400655/java-comparator-documentation-confused-about-the-terminology-total-order

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.