Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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]

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.

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));
                }
            });

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 ♨︎].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top