Implements Comparable to get alphabetical sort with Strings

后端 未结 4 868
离开以前
离开以前 2021-02-20 14:30

I would like an object to be comparable (to use it in a TreeSet in that case).

My object got a name field and I would like it to be sorted by alphabetical order.

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-20 14:40

    return name.compareTo(otherObject.name);
    

    String already implements Comparable so you don't need do to anything.

提交回复
热议问题