0

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

INPUT:

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)

OUTPUT:

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

How could I proceed through it ?

Rafael Ferreira
  • 329
  • 2
  • 16

2 Answers2

1

This will sort.. even if your individual list size is more than 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);
    }
    }
}
Aditya Rewari
  • 2,343
  • 2
  • 23
  • 36
0

You could think of each List of String as a single Pojo that contains a team, country, and player. Then sort a list based on the team. You could of course sort by country if needed, or player if needed.

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

Output:

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
Jason
  • 5,154
  • 2
  • 12
  • 22