3

I have a list of list as follow:

List<List<Integer>> matchedPostions = findTerms(originalEntPos, singularEntPos, singText);

Consider this example

[ID,StartPostion,EndPostion]
      ^^^
[1,198,200]
[2,50,61]

I am trying to sort list using Collections.sort(). How can I sort the values inside the matchedPostions based on the StartPostion values, from lower to higher values?

3
  • 1
    you are trying to sort user defined objects. You will have to use Comparator or comparable interface. These links will help you.thejavageek.com/2013/06/17/sorting-user-defined-objects-part-1 Commented May 12, 2014 at 12:26
  • 2
    This does not look like a list of lists of integers. This would be a list of lists: [1,198,200], [2,50,61] Commented May 12, 2014 at 12:28
  • 1
    The inner list looks like a coordinate value, why not define a Class for it. Commented May 12, 2014 at 12:34

5 Answers 5

4

You'll need to implement a Comparator to sort custom data-structures like the one you provided.

import static java.util.Arrays.asList;

List<List<Integer>> matchedPostions = asList(asList(1, 198, 200), asList(2, 50, 61));
Collections.sort(matchedPostions, new Comparator<List<Integer>>() {
    @Override
    public int compare(List<Integer> o1, List<Integer> o2) {
        // Sort the lists using the starting position (second element in the list)
        return o1.get(1).compareTo(o2.get(1));
    }
});

System.out.println(matchedPostions);
// [[2, 50, 61], [1, 198, 200]]

This is the "dirty" way. The more idiomatic approach is described Duncan where you implement a Range class which properly encapsulates your data.

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

Comments

3

I would strongly recommend you create a class to hold your list values. This allows you to enjoy the benefits of type safety, including being sure you always have exactly two integer values (rather than a unknown number of items in a list). For instance:

public class Range implements Comparable<Range> {
    private final int startPosition;
    private final int endPosition;

    public Range(int startPosition, int endPosition) {
        this.startPosition = startPosition;
        this.endPosition = endPosition;
    }

    @Override
    public int compareTo(Range o) {
        return startPosition - o.startPosition;
    }

    @Override
    public String toString() {
        return String.format("[%d,%d]", startPosition, endPosition);
    }
}

Because this class implements Comparable, you can sort with the normal Collections.sort method:

public static void main(String[] args) throws Exception {
    List<Range> ranges = Arrays.asList(new Range(198, 200), new Range(50,
            61));

    System.out.println("Unsorted");
    for (Range range : ranges) {
        System.out.println(range);
    }

    Collections.sort(ranges);

    System.out.println("Sorted");
    for (Range range : ranges) {
        System.out.println(range);
    }
}

Output:

Unsorted
[198,200]
[50,61]
Sorted
[50,61]
[198,200]

Comments

2

For the inner lists, you can just loop over them:

for(List<Integer> inner : outer){
    Collections.sort(inner);
}

For the outer List, you need a custom Comparator.

Comments

1

If you can't define your own specialized classes for ranges, your can call Collections.sort with a your own Comparator. Example is below:

Collections.sort(list, new Comparator<List<Integer>>() {
                @Override
                public int compare(List<Integer> l1, List<Integer> l2) {
                    return l1.get(0).compareTo(l2.get(0));
                }
            });

Comments

0

Using sort() in java you can sort easily using lambda function: List<List> ans = new LinkedList<>();

ans.sort((x, y) -> {
        for (int i = 0; i < Math.min(x.size(), y.size()); i++) {
            if (x.get(i) != y.get(i)) {
                return x.get(i) - y.get(i);
            }
        }
        return x.size() - y.size();
    });

Its another way for sorting List of List of Integer using lambda function [Upvote for more such Java solution ♨︎].

1 Comment

Its another way for sorting List of List of Integer using lambda function [Upvote for more such Java solution ♨︎].

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.