Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
677 views
in Technique[技术] by (71.8m points)

sorting - Java sort ArrayList with custom fields by number and alphabetically

public class Product implements Serializable{

    private String id;
    private String name;
    private double price ;
    private int quantity;

    public Product(String id, String name, double price, int quantity) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.quantity = quantity;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    @Override
    public String toString() {
        return "Product{" + "id=" + id + ", name=" + name + ", price=" + price + ", quantity=" + quantity + '}';
    }

I want to sort ArrayList<Product> by price and name. I search Google for a long time but I cann't solve it. Whether it can have problem with Serializable

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to implement Comparable or Comparator interface for your purpose. sorting user defined objects with Comparator and sorting user defined objects with comparable

You can learn the difference between these two by reading these tutorials

Consider you want your products to be sorted using its price then make your Product implement Comparable as follows

public class Product implements Comparable<Product>{

    public int compareTo(Product other){
       // your logic here
    }

}

But hold on... now that we have already implemented Comparable interface to sort the objects using its price, how can we sort them using another sort sequence? We only have one compareTo() method and we can't write separate sort sequence in the same class. Here comes the role of Comparator. With Comparator, you can define multiple sort sequences.

Suppose we want to sort using its price, then:

public class PriceSorter implements Comparator<Product>{

    public int compare(Product one, Product another){
        int returnVal = 0;

    if(one.getPrice() < another.getPrice()){
        returnVal =  -1;
    }else if(one.getPrice() > another.getPrice()){
        returnVal =  1;
    }else if(one.getPrice() == another.getPrice()){
        returnVal =  0;
    }
    return returnVal;
    }
}

and you want another sort sequence, now for its name, then:

public class NameSorter implements Comparator<Product>{

        public int compare(Product one, Product another){
            return one.getName().compareTo(another.getName());
        }
}

Now, when you want to sort using price, then

Collections.sort(yourList,new PriceSorter());

If you want to sort using name, then

Collections.sort(yourList, new NameSorter());

The second argument takes the Comparator instance which makes the sort method know what logic to follow while sorting objects


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...