java - 为了对对象进行排序,什么必须为真?

标签 java algorithm sorting

所以我的标题就是我的问题...为了对对象进行排序,基本要求或必须满足的条件是什么? 以下是一些选择:

一个。它们必须是基本类型(Integer、Double 等)的包装类

B.它们必须具有可比性。

C.至少一个实例变量必须是原语。

D.它们必须是字符串

E.对象不可排序,只有基元可排序。

最佳答案

B.比较。

java documentation for Comparable 中快速搜索揭示:

This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.

Lists (and arrays) of objects that implement this interface can be sorted automatically by Collections.sort (and Arrays.sort). Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comparator.

关于java - 为了对对象进行排序,什么必须为真?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53699573/

相关文章:

algorithm - 什么是几何算法的好来源?

c# - 根据连续调用之间耗时优化批量大小

python - 求解 x^2 + y^2 + z^2 = N 得到 x, y, z 的所有唯一组合

mysql - 对一个 mysql 表进行排序,删除它并创建一个新的排序表

java - 如何避免Google guice注入(inject)器getInstance()由于null而重复调用?

java - arrayadapter 的 SetAdapter 崩溃

Java SecurityManager 防止 .. 目录使用

java - 如何在Android App中获取两个日期之间的时差?

linux - 使用 Linux 排序命令对多个键进行排序

c++ - 对耦合的两个链表运行归并排序