How to sort an array of objects containing null elements?

后端 未结 5 1830
梦谈多话
梦谈多话 2020-11-27 23:24

In my program an array fClasses of fixed length [7] of objects is created, each object is a class FClass that contains 3 Strings, an <

5条回答
  •  感动是毒
    2020-11-27 23:59

    Using Java 8, you can easily build the comparator you need:

    Arrays.sort(fClasses, Comparator.nullsFirst(Comparator.naturalOrder()));
    

    Use nullsLast instead if that's what you want, of course.

提交回复
热议问题