1

In Java, I have a List of List that looks something like the following:

[ ["Kelly", "3.0"], ["Jeff", "2.0"], ["Mark", "1.0]" ]

How do I sort the list by a certain index? In this case, index of 1

Ex)

Given:   [ ["Kelly", "3.0"], ["Jeff", "2.0"], ["Mark", "1.0"] ]
Desired: [ ["Mark", "1.0"], ["Jeff", "2.0"], ["Kelly", "3.0"] ]

What I'm looking for is something very similar to the 'sorted()' method in Python (the exact solution I'm looking for in Python shown here: https://www.kite.com/python/answers/how-to-sort-a-list-of-lists-by-an-index-of-each-inner-list-in-python and here: sort list of lists by specific index of inner list) but I'm looking for a solution in Java.

Also as a bonus, how would I sort these in descending order? What about instances of same values (i.e. there's multiple '3.0' values)

1
  • 1
    You can use a custom comparator that compares the second element of each list to determine sort order. It's simply a matter of flipping the variables around to achieve descending order. Commented Mar 21, 2021 at 7:45

3 Answers 3

2

Since Java 8 there is method List::sort accepting a custom Comparator which can be built using Comparator.comparing.

So, the input list of lists may be concisely sorted as follows:

List<List<String>> data = Arrays.asList(
    Arrays.asList("Kelly", "3.0"),
    Arrays.asList("Mark", "1.0"),
    Arrays.asList("Jeff", "2.0")
);

// sorting by index 1
data.sort(Comparator.comparing(x -> x.get(1))); 
// [[Mark, 1.0], [Jeff, 2.0], [Kelly, 3.0]]

// sorting by index 1 in reverse order using Collections.reverseOrder
data.sort(Comparator.comparing(x -> x.get(1), Collections.reverseOrder()));
// [[Kelly, 3.0], [Jeff, 2.0], [Mark, 1.0]]

// sorting by index 0 in reverse order using Comparator.reversed()
// here type of object being compared needs to be specified
data.sort(Comparator.comparing((List<String> x) -> x.get(0)).reversed());
// [[Mark, 1.0], [Kelly, 3.0], [Jeff, 2.0]]
Sign up to request clarification or add additional context in comments.

Comments

2

Try this.

List<List<String>> list = Arrays.asList(
    Arrays.asList("Jhon", "3.0"), Arrays.asList("Kelly", "3.0"),
    Arrays.asList("Jeff", "2.0"), Arrays.asList("Mark", "1.0"));
//  sort the list by index of 1
list.sort(Comparator.comparing(x -> x.get(1)));
System.out.println(list);
//  sort the list by index of 1 descending order
list.sort(Collections.reverseOrder(Comparator.comparing(x -> x.get(1))));
System.out.println(list);

output:

[[Mark, 1.0], [Jeff, 2.0], [Jhon, 3.0], [Kelly, 3.0]]
[[Jhon, 3.0], [Kelly, 3.0], [Jeff, 2.0], [Mark, 1.0]]

It is stable sort in both cases.

Comments

0

Solved my own question literally 2 minutes after posting. Always have to love when that happens lol:

    for (int i=0; i < listToReverse.size(); i++) {
        for (int j=0; j < (listToReverse.size()-i-1); j++) {
            if ((int)Double.parseDouble(listToReverse.get(j).get(1)) < (int)Double.parseDouble(listToReverse.get(j+1).get(1))) {
                ArrayList<String> temp = listToReverse.get(j);
                listToReverse.set(j, asList.get(j+1));
                listToReverse.set(j+1, temp);
            }
        }
    }
//This implementation sorts in descending order. Switch the '<' for a '>' to sort in ascending order

Thank you to @bliss for also pointing out a custom comparator could be used here. Probably much prettier than the iterative way

1 Comment

Applying a custom comparator is as simple as listToReverse.sort((list1, list2) -> list1.get(1).compareTo(list2.get(1))); and it utilizes a far more efficient library sorting algorithm.

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.