且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Java中排序我自己类型的数组列表

更新时间:2022-11-19 12:56:13

声明项目可比较,并实施 comapreTo 方法比较 顺序中 itemName (即将 >此,而不是正常的 )。

Declare Item to be Comparable, and implement the comapreTo method to compare itemName in reverse order (ie compare "that to this", rather than the normal "this to that").

像这样:

public class Item implements Comparable<Item> {
    private Integer itemNo;
    private String itemName;
    private String itemDescription;

    public int compareTo(Item o) {
        return o.itemName.compareTo(itemName); // Note reverse of normal order
    }

    // rest of class
}