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