1

I have a Custom Object Suppose Team

public class Team{
    String      name;
    int         teamNo;
    ArrayList<Team> innerTeams;
    int teamId;

    //Getters and Setter Methods

Now I want to Sort it in Ascending Order of First Property name taking into account that each Team Object has a property of itself as Team as arraylist declared as innerTeams How can I be able to Sort this. So utlimately when any arrayList of object Team is present it should be sorted.

Please anyone help me with this.

  • https://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property?rq=1 – Artur Biesiadowski Oct 06 '17 at 14:17
  • sort the top array list then the inner ones (something like that) –  Oct 06 '17 at 14:18
  • I'm not clear on what you're trying to do. Are you trying to sort a `List` of `Team` objects based on their `innerTeams`? Seems like if you're comparing `Team` objects you would compare by name. – D.B. Oct 06 '17 at 14:26
  • @D.B. Infact I'm trying to Sort the List of `Team` by its `name`. Bu then Since it has `innerTeam` as an Property I want the `innerTeam` also to be sorted. –  Oct 09 '17 at 05:32
  • @JohnHumanyun in that case see [the answer by SergeyB](https://stackoverflow.com/a/46607948/3284624). Implementing `Comparable` allows you to sort any `List` of `Team` objects. If you want the `innerTeams` to be sorted then your `Team` class should handle sorting it as needed based on your requirements - for example sort it when returning it in a getter method, etc. The `Team` class would call the `Collections.sort` method internally in order to take advantage of its own `compareTo` method. E.g. `public List getInnerTeams(){ Collections.sort(innerTeams); return innerTeams;}` – D.B. Oct 09 '17 at 16:15

2 Answers2

1

The easiest way is to have your Team class implement Comparable. You can tweak the logic inside of the compareTo to match your needs, e.g. compare inner team names, etc. Then you use Collections.sort() to do the actual sorting.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Team implements Comparable<Team> {

    String name;
    int teamNo;
    List<Team> innerTeams = new ArrayList<>();
    int teamId;

    @Override
    public int compareTo(Team o) {
        if(o == null) {
            return 1;
        } else if(name == null) {
            return 0;
        } else {
            return name.compareTo(o.name);
        }
    }

    public static void main(String[] args) {
        Team team1 = new Team();
        team1.name = "z";
        Team team2 = new Team();
        team2.name = "a";

        List<Team> teams = new ArrayList<>();
        teams.add(team1);
        teams.add(team2);

        System.out.println(teams);

        Collections.sort(teams);

        System.out.println(teams);
    }

    @Override
    public String toString() {
        return name;
    }
}

Output:

[z, a]
[a, z]

You can then also use the same approach to sort innerTeams by name if needed.

SergeyB
  • 9,478
  • 4
  • 33
  • 47
0

You should define Comparator of Team.

class TeamComparator implements Comparator<Team>{
  public int compare(Team o1,Team o2){ 
    return o1.name.compareTo(o2.name);  
  }
}

And sort by Collections.sort(teamList, new TeamComparator())

  • `o1.name` assumes `o1` is not null and similarly `o2.name` assumes `o2` is not null. – D.B. Oct 06 '17 at 14:41