如何在Java中对ArrayList进行排序 - How to sort an ArrayList in Java [duplicate]

10/14 12:01
阅读数 0

问题:

This question already has answers here : 这个问题已经在这里有了答案
Closed 6 years ago . 6年前关闭。

I have a class named Fruit. 我有一堂课叫做水果。 I am creating a list of this class and adding each fruit in the list. 我正在创建此类的列表,并将每个水果添加到列表中。 I want to sort this list based on the order of fruit name. 我想根据水果名称的顺序对该列表进行排序。

public class Fruit{

    private String fruitName;
    private String fruitDesc;
    private int 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;
    }
}

and I am creating its list using for loop 我正在使用for循环创建它的列表

List<Fruit>  fruits= new ArrayList<Fruit>();

Fruit fruit;
for(int i=0;i<100;i++)
{
   fruit = new fruit();
   fruit.setname(...);
   fruits.add(fruit);
}

and I need to sort this arrayList using the fruit name of each object in the list 我需要使用列表中每个对象的水果名称对该arrayList进行排序

how?? 怎么样??


解决方案:

参考一: https://en.stackoom.com/question/1FNZ8
参考二: https://stackoom.com/question/1FNZ8
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部