Java按字母顺序按自定义字段排序ArrayList [英] 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 + '}';
}
我想排序 ArrayList< Product>
按价格和名称。我搜索谷歌很长一段时间但我无法解决它。是否可以解决可序列化
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
推荐答案
您需要实现可比较
或 Comparator
界面,用于您的目的。 使用Comparator对用户定义的对象进行排序和对用户定义的对象进行排序
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
}
}
但坚持......现在我们已经实现了 Comparable
界面来使用其价格对对象进行排序,我们如何使用其他排序顺序对它们进行排序?我们只有一个 compareTo()
方法,我们不能在同一个类中编写单独的排序序列。这里有 Comparator
的角色。使用 Comparator
,您可以定义多个排序顺序。
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());
第二个参数采用 Comparator
实例使排序方法知道在排序对象时要遵循的逻辑
The second argument takes the Comparator
instance which makes the sort method know what logic to follow while sorting objects
这篇关于Java按字母顺序按自定义字段排序ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!