使用 Collections.sort(arrayListName) 对包含对象的 ArrayList 按其 ID 进行排序

4
我有一个库存类,它创建了一个ArrayList,其中装满了Item对象,Item也是一个类。我知道我必须调用Collections.sort(items)来对ArrayList进行排序(顺便说一下,这个ArrayList的名字叫做items)。作业要求我在Item类上使用一个接口,但我不知道是否应该实现Comparator或Comparable,然后分别为compareTo()方法或compare()方法编写什么内容。此外,我在声明ArrayList之后就调用了Collections.sort(items)方法,这样做可以吗?
编辑:我的老师刚刚澄清,她希望我们在Item类上实现Comparable接口。
3个回答

5

自Java 8以来:

List<Item> items = new ArrayList<>();
// add elements
Collections.sort(items, Comparator.comparingLong(Item::getId));

1
你需要实现Comparable接口,然后使用Collections.sort方法即可排序。
如果你需要一个新的比较器而不想使用Comparable接口,你可以创建一个新的Comparator并像这样使用:Collections.sort(list, new MyComparator())
public class Fruit implements Comparable<Fruit>{
    
    private String fruitName;
    private String fruitDesc;
    private int quantity;
    
    public Fruit(String fruitName, String fruitDesc, int quantity) {
        super();
        this.fruitName = fruitName;
        this.fruitDesc = fruitDesc;
        this.quantity = quantity;
    }
    
    public String getFruitName() {
        return fruitName;
    }
    public void setFruitName(String fruitName) {
        this.fruitName = fruitName;
    }
    public String getFruitDesc() {
        return fruitDesc;
    }
    public void setFruitDesc(String fruitDesc) {
        this.fruitDesc = fruitDesc;
    }
    public int getQuantity() {
        return quantity;
    }
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public int compareTo(Fruit compareFruit) {
    
        int compareQuantity = ((Fruit) compareFruit).getQuantity(); 
        
        //ascending order
        return this.quantity - compareQuantity;
        
        //descending order
        //return compareQuantity - this.quantity;
        
    }   
}

来源:mkyong


所以我有一个实现了Comparable<Item>接口的Item类,然后在这个类中我定义了compareTo()方法:public int compareTo (Item other) { if (this.id.compareToIgnoreCase((other.getId())) == 0) return 0; else if (this.id.compareToIgnoreCase(other.getId()) > 0) return 1; else return -1; }那么现在我只需要在我的Inventory类中调用Collections.sort(items)吗? - bassandguitar
我也是这么认为的,但在我写Collections.sort(items);时,它报错了。错误提示如下:
  • Syntax error on token "items", VariableDeclaratorId expected after this token
  • Syntax error on token(s), misplaced construct(s)
- bassandguitar
等等,我明白了,我把它放在类的主体中了,谢谢! - bassandguitar
我厌恶带链接的解决方案。这不是一个解决方案。你给出的链接中有一个已经失效了。 - trixo
@trixo 已更新。已删除错误链接,并添加了一个链接中的简要实现,指定了来源 :) - Alex

0

Collections.sort() 有两个版本。

Collections.sort(List)Collections.sort(List,Comparator)。一个接受一个List,另一个接受一个List和一个Comparator实例。单参数的sort()方法期望你的类已经实现了Comparable并重写了compareTo()方法。而双参数的sort()方法则需要一个Comparator实例,其中你已经实现了Comparator并重写了它的compare()方法。

如果一个对象的自然排序方式是明确的,并且任何需要对该类进行排序的人通常都希望以这种方式进行排序,则该对象应实现Comparable接口。

但是,如果排序是该类的不寻常用例或者可以有多个排序顺序,则使用Comparator更好。

此外,在我声明ArrayList之后,我立即调用了Collections.sort(items)方法,这样做可以吗?

当您需要对List进行排序时,才会调用sort()方法。在调用sort()方法之后添加项目不会自动对List进行排序,以适应由于添加新元素而导致的列表项排序更改。

示例实现:

public int compareTo (Item other) { 
   return new Integer(this.getID()).compareTo(new Integer(other.getID()));
}

我必须使用只调用Collections.sort(items)的那个。 - bassandguitar
好的,但基于什么领域? - AllTooSir
你的意思是什么?抱歉,我不理解。 - bassandguitar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接