2

Hi i need to sort array two Dimension in java but i need the sorting to be by index inside my array my array is

  String arr[][]={  {"joun","1525565","10"},
                           {"ALI","15256562","100"},
                           {"FATEH","1525534","20"}   };

I need to sort my array by index number two and the output is

   String arr[][]={ {"ALI","15256562","100"},
                       {"FATEH","1525534","20"},
                         {"joun","1525565","10"} };

my order must be 100 then 20 then 10

i tried to put this code in java put the problem the program get confused from the index 1 and two i used inside for loop Arrays.sort(arr[i])

3
  • possible duplicate of stackoverflow.com/questions/16846301/… Commented Apr 27, 2015 at 16:06
  • if it's work with Arrays.sort it will be better but i think it's not working with this method Commented Apr 27, 2015 at 16:09
  • Look at Peter Lawrey's answer for Arrays.sortin Java 8 Commented Apr 27, 2015 at 16:10

2 Answers 2

5

In Java 8 you would write this to sort by index 2 for decreasing numeric order with

Arrays.sort(arr, Comparator.comparing(row -> -Long.parseLong(row[2])));

or

Arrays.sort(arr, Comparator.comparing((String[] row) -> Long.parseLong(row[2]))
                           .reversed());

For ascending order as a String rather than a number.

Arrays.sort(arr, Comparator.comparing(row -> row[2]));

This would order 10, 100, 20 but if you made longer strings before shorter ones.

Arrays.sort(arr, Comparator.comparing((String[] row) -> row[2].length()).reversed()
                           .thenComparing(row->row[2]));

This would give the order as 100, 10, 20

BTW: I don't know why the first row needs the type given.

if you wanted to sort by number you could do

Arrays.sort(arr, Comparator.comparing(row -> Long.parseLong(row[2])));

This would put 10, 20, 100 in increasing order.

In Java 8 you can add an anonymous inner class to do the same thing.

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

12 Comments

what is row here ? is it the array name ? because i get an error
@user44444 are using using Java 8? row is the name of the nested array. What error do you get? This is using Java 8's lambdas and Streams API.
ok i changed my project to 8 and there is no error but the code is not working with me i wrote Arrays.sort(arr, Comparator.comparing(row -> row[2])); for (String[] item : arr) { System.out.println(Arrays.toString(item)); } and i get the same last array
the error is Type mismatch: cannot convert from String to Comparable<? super Comparable<? super U>>
@user44444 I tried three different ways to sort the numbers. ;) can explain the sort order you want. Is it decreasing numeric order?
|
1

Here is the solution with Comparator:

public class SortTest {
    public static void main(String[] args) {
        String arr[][]={  {"joun","1525565","10"},
                {"ALI","15256562","100"},
                {"FATEH","1525534","20"}   };

        Arrays.sort(arr, new Comparator<String[]>() {
            @Override
            public int compare(String[] o1, String[] o2) {
                return Integer.parseInt(o2[2]) - Integer.parseInt(o1[2]);
            }
        });

        for (String[] item : arr) {
            System.out.println(Arrays.toString(item));
        }
    }
}

4 Comments

@user44444 "it sort every thing" <- what do you mean?
i mean it sort the number 1525565 and 10 my array now is {"joun","1525565","100"}, but now it's {"joun","1525565","10"} array[0] was {"joun","1525565","100"}
@user44444 in your initial example it is: {"joun","1525565","10"} sorted output of my code is: [ALI, 15256562, 100] [FATEH, 1525534, 20] [joun, 1525565, 10] where is the issue?
OK the problem was from me because my array is not the same what i wrote in the example but it similar i edit some thing in your code and it's working for me now your code is working immediately with my example above thanks :)

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.