How to simplify a null-safe compareTo() implementation?

前端 未结 17 2081
醉酒成梦
醉酒成梦 2020-11-28 18:03

I\'m implementing compareTo() method for a simple class such as this (to be able to use Collections.sort() and other goodies offered by the Java pl

17条回答
  •  抹茶落季
    2020-11-28 18:40

    Another Apache ObjectUtils example. Able to sort other types of objects.

    @Override
    public int compare(Object o1, Object o2) {
        String s1 = ObjectUtils.toString(o1);
        String s2 = ObjectUtils.toString(o2);
        return s1.toLowerCase().compareTo(s2.toLowerCase());
    }
    

提交回复
热议问题