【问题标题】:How to sort list of multiple list in Java [duplicate]如何在Java中对多个列表的列表进行排序[重复]
【发布时间】:2020-03-06 13:42:16
【问题描述】:

我想根据许多列对列表进行排序,但我不知道如何进行。

输入

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

输出:

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

我该如何处理它?

【问题讨论】:

标签: java sorting arraylist


【解决方案1】:

这将排序..即使您的个人列表大小超过 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);
    }
    }
}

【讨论】:

【解决方案2】:

您可以将每个字符串列表视为一个包含团队、国家和球员的 Pojo。然后根据团队对列表进行排序。如果需要,您当然可以按国家/地区或玩家(如果需要)进行排序。

   private static final class Entity {

        private final String team;

        private final String country;

        private final String player;

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

        public String getTeam() {
            return team;
        }

        public String getCountry() {
            return country;
        }

        public String getPlayer() {
            return player;
        }

        @Override
        public String toString() {
            return String.format("team=%s, country=%s, player=%s", team, country, player);
        }
    }

    public static void main(String[] args) {
        Entity a = new Entity("TEAMA", "COUNTRYA", "PLAYERA");

        Entity b = new Entity("TEAMB", "COUNTRYF", "PLAYERB");

        Entity c = new Entity("TEAMC", "COUNTRYR", "PLAYERC");

        Entity d = new Entity("TEAMB", "COUNTRYA", "PLAYERD");

        Entity e = new Entity("TEAMA", "COUNTRYA", "PLAYERE");

        Entity f = new Entity("TEAMA", "COUNTRYF", "PLAYERF");

        List<Entity> sorted = new ArrayList<>(Arrays.asList(a, b, c, d, e, f))
                .stream().sorted(Comparator.comparing(Entity::getTeam))
                .collect(Collectors.toList());

        sorted.forEach(System.out::println);
    }

输出:

team=TEAMA, country=COUNTRYA, player=PLAYERA
team=TEAMA, country=COUNTRYA, player=PLAYERE
team=TEAMA, country=COUNTRYF, player=PLAYERF
team=TEAMB, country=COUNTRYF, player=PLAYERB
team=TEAMB, country=COUNTRYA, player=PLAYERD
team=TEAMC, country=COUNTRYR, player=PLAYERC

【讨论】:

  • 糟糕,搞错了,已编辑。
猜你喜欢
  • 2020-12-23
  • 2014-12-26
  • 1970-01-01
  • 2016-10-20
  • 2018-02-12
  • 2021-02-09
  • 2022-01-24
  • 1970-01-01
  • 2011-07-07
相关资源
最近更新 更多