How to sort List of List in 3 Categories [closed]

2020-05-09 23:13发布

问题:

I want to sort a list based on many columns but I do not know how to proceed.

INPUT:

List<String> a = ["TEAMA", "COUNTRYA", "AGEA", "PLAYERA"]
List<String> b = ["TEAMB", "COUNTRYF", "AGEA", "PLAYERB"]
List<String> c = ["TEAMC", "COUNTRYR", "AGEB", "PLAYERC"]
List<String> d = ["TEAMB", "COUNTRYF", "AGEC", "PLAYERD"]
List<String> e = ["TEAMA", "COUNTRYA", "AGEA", "PLAYERE"]
List<String> f = ["TEAMA", "COUNTRYF", "AGEE", "PLAYERF"]
List<List<String>> FinalList = []
FinalList.add(a)
FinalList.add(b)
FinalList.add(c)
FinalList.add(d)
FinalList.add(e)
FinalList.add(f)

OUTPUT:

["TEAMA", "COUNTRYA", "AGEA", "PLAYERA"]
["TEAMA", "COUNTRYA", "AGEA", "PLAYERE"]
["TEAMA", "COUNTRYF", "AGEE", "PLAYERF"]
["TEAMB", "COUNTRYF", "AGEA", PLAYERB"]
["TEAMB", "COUNTRYF", "AGEC", "PLAYERD"]
["TEAMC", "COUNTRYR", "AGEB", "PLAYERC"]

How could I proceed through it ?

回答1:

You can create as many comparators as you have columns and use one at a time or chain them to sort by multiple sorting criteria:

Comparator<List<String>> byIndex0 = Comparator.comparing(i -> i.get(0));
Comparator<List<String>> byIndex1 = Comparator.comparing(i -> i.get(1));
Comparator<List<String>> byIndex2 = Comparator.comparing(i -> i.get(2));
Comparator<List<String>> byIndex3 = Comparator.comparing(i -> i.get(3));

Sort your final list by the deseired criteria, eg. by the first index (Team)

FinalList.sort( byIndex0 );

To sort by two criterias you can just chain them using #thenComparing, eg. by team an then by country

FinalList.sort( byIndex0.thenComparing(byIndex1));

Or use all:

FinalList.sort( byIndex0.thenComparing(byIndex1).thenComparing(byIndex2).thenComparing(byIndex3));


回答2:

I already provided an answer in one of your previous posts. Instead of having a List of String we represent this as a plain old java object called Entity (give it a better name). Then we create a List from all Entity objects, and sort based on the team. If you want to sort by country, or player, you easily can.

  1. Create a POJO to represent those String values.
   private static final class Entity {

        private final String team;

        private final String country;

        private final String age;

        private final String player;

        private Entity(String team, String country, String age, String player) {
            this.team = team;
            this.country = country;
            this.age = age;
            this.player = player;
        }

        public String getTeam() {
            return team;
        }

        public String getCountry() {
            return country;
        }

        public String getAge() {
            return age;
        }

        public String getPlayer() {
            return player;
        }
    }

Create some Comparator objects to re-use so you can sort by different attributes.

    private static final Comparator<Entity> SORT_BY_TEAM = Comparator.comparing(Entity::getTeam);

    private static final Comparator<Entity> SORT_BY_COUNTRY = Comparator.comparing(Entity::getCountry);

    private static final Comparator<Entity> SORT_BY_AGE = Comparator.comparing(Entity::getAge);

    private static final Comparator<Entity> SORT_BY_PLAYER = Comparator.comparing(Entity::getPlayer);
    public static void main(String[] args) {
        List<Entity> entities = new ArrayList<>(Arrays.asList(
                new Entity("TEAMA", "COUNTRYA", "AGEA", "PLAYERA"),
                new Entity("TEAMA", "COUNTRYA", "AGEA", "PLAYERE"),
                new Entity("TEAMA", "COUNTRYF", "AGEE", "PLAYERF"),
                new Entity("TEAMB", "COUNTRYF", "AGEA", "PLAYERB"),
                new Entity("TEAMB", "COUNTRYF", "AGEC", "PLAYERD"),
                new Entity("TEAMC", "COUNTRYR", "AGEB", "PLAYERC")));

        List<Entity> sortedByTeam = entities.stream().sorted(SORT_BY_TEAM).collect(Collectors.toList());

        List<Entity> sortedByAge = entities.stream().sorted(SORT_BY_AGE).collect(Collectors.toList());

        List<Entity> sortedByCountry = entities.stream().sorted(SORT_BY_COUNTRY).collect(Collectors.toList());

        List<Entity> sortedByPlayer = entities.stream().sorted(SORT_BY_PLAYER).collect(Collectors.toList());
    }


回答3:

public class SortListExample {
    public static void main(String[] args) {
        List<String> a = Arrays.asList("TEAMA", "COUNTRYA", "PLAYERA");
        List<String> b = Arrays.asList("TEAMB", "COUNTRYF", "PLAYERB");
        List<String> c = Arrays.asList("TEAMC", "COUNTRYR", "PLAYERC");
        List<String> d = Arrays.asList("TEAMB", "COUNTRYA", "PLAYERD");
        List<String> e = Arrays.asList("TEAMA", "COUNTRYA", "PLAYERE");
        List<String> f = Arrays.asList("TEAMA", "COUNTRYF", "PLAYERF");
        List<List<String>> FinalList = new ArrayList<>();
        FinalList.add(a);
        FinalList.add(b);
        FinalList.add(c);
        FinalList.add(d);
        FinalList.add(e);
        FinalList.add(f);
        List<List<String>> listToSort = new ArrayList<>(FinalList);
        listToSort.sort((l1, l2) -> {
            int i = 0;
            while (true) {
                if (l1.get(i) != null && l2.get(i) != null) {
                    int compareVal = l1.get(i).compareTo(l2.get(i));
                    if (compareVal != 0) {
                        return compareVal;
                    }
                    i++;
                }
            }
        });
        for (List<String> list: listToSort) {
        System.out.println(list);
    }
    }
}


回答4:

you would simply have to retrieve the values of the linked List and then store and sort values retrieves from the same column(or index). Also make sure you have all quotation marks in code.