Sorting a List alphabetically using compareTo() method

前端 未结 4 1036
星月不相逢
星月不相逢 2021-01-18 08:37

I am writing a phonebook program in java and i need to list people in the list alphabetically and to do that i need to write a sorting algorithm for a list in java and it sh

4条回答
  •  鱼传尺愫
    2021-01-18 09:06

    Implement Comparable in your Person class.

    Your compareTo() method would then be something like:

    public int compareTo(Person other) {
        return name.compareTo(other.getName())
    }
    

    Then use Collections.sort();

提交回复
热议问题