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>
进行排序。我搜索谷歌很长一段时间但我无法解决它。是否可以解决 Serializable
答案 0 :(得分:28)
您需要为您的目的实施Comparable
或Comparator
界面。 sorting user defined objects with Comparator和sorting user defined objects with comparable
通过阅读这些教程,您可以了解这两者之间的区别
考虑您希望使用其价格对产品进行排序,然后按照以下方式制作Product
工具Comparable
public class Product implements Comparable<Product>{
public int compareTo(Product other){
// your logic here
}
}
但请坚持......现在我们已经实现了Comparable
接口来使用其价格对对象进行排序,我们如何使用其他排序顺序对它们进行排序?我们只有一个compareTo()
方法,我们不能在同一个类中编写单独的排序顺序。 Comparator
的角色来了。使用Comparator
,您可以定义多个排序顺序。
假设我们想要使用其价格进行排序,那么:
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;
}
}
你想要另一个排序顺序,现在是它的名字,然后:
public class NameSorter implements Comparator<Product>{
public int compare(Product one, Product another){
return one.getName().compareTo(another.getName());
}
}
现在,当您想使用价格进行排序时,
Collections.sort(yourList,new PriceSorter());
如果要使用名称排序,则
Collections.sort(yourList, new NameSorter());
第二个参数采用Comparator
实例,这使得sort方法知道在排序对象时要遵循的逻辑
答案 1 :(得分:5)
让Product
类实现Comparable
接口。
public class Product implements Serializable, Comparable<Product> {
//Ommitted constructors, fields and accessors
//This is an ascending sort order
@Override
public int compareTo(Product o) {
int result = this.name.compareToIgnoreCase(o.name);
if(result != 0){
return result;
}else{
return new Double(this.price).compareTo(new Double(o.price));
}
}
}
然后排序就像通过List
到Collections.sort()
:
public static void main(String[] args) {
Product p1 = new Product("p1", "shoes", 30.33, 20);
Product p2 = new Product("p2", "shoes", 20.30, 20);
Product p3 = new Product("p3", "shoes", 50.33, 20);
Product p4 = new Product("p4", "socks", 10.50, 20);
Product p5 = new Product("p5", "socks", 5.40, 20);
Product p6 = new Product("p6", "socks", 2.34, 20);
List<Product> products = Arrays.asList(p1,p2,p3,p4,p5,p6);
System.out.println("Unsorted");
for(Product product:products){
System.out.println("Product: " + product.name + " Price: " + product.price);
}
Collections.sort(products);
System.out.println("sorted");
for(Product product:products){
System.out.println("Product: " + product.name + " Price: " + product.price);
}
}
Product
的{{1}}的完整来源,其中使用Comparable
方法中的排序示例实现了main
:
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Product implements Serializable, Comparable<Product> {
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 + '}';
}
@Override
public int compareTo(Product o) {
int result = this.name.compareToIgnoreCase(o.name);
if(result != 0){
return result;
}else{
return new Double(this.price).compareTo(new Double(o.price));
}
}
public static void main(String[] args) {
Product p1 = new Product("p1", "shoes", 30.33, 20);
Product p2 = new Product("p2", "shoes", 20.30, 20);
Product p3 = new Product("p3", "shoes", 50.33, 20);
Product p4 = new Product("p4", "socks", 10.50, 20);
Product p5 = new Product("p5", "socks", 5.40, 20);
Product p6 = new Product("p6", "socks", 2.34, 20);
List<Product> products = Arrays.asList(p1,p2,p3,p4,p5,p6);
System.out.println("Unsorted");
for(Product product:products){
System.out.println("Product: " + product.name + " Price: " + product.price);
}
Collections.sort(products);
System.out.println("sorted");
for(Product product:products){
System.out.println("Product: " + product.name + " Price: " + product.price);
}
}
}
答案 2 :(得分:3)
使用Comparator<Product>
,此处匿名实施(适用于java 7及更早版本):
List<Product> list;
Collections.sort(list, new Comparator<Product>() {
public int compare(Product a, Product b) {
if (a.getPrice() == b.getPrice())
return a.getName().compareTo(b.getName());
return a.getPrice() > b.getPrice() ? 1 : a.getPrice() < b.getPrice() ? -1 : 0
}
});
Java 8有一种更清晰的方法来实现上述目标:
Collections.sort(list, Comparator.comparing(Product::getPrice).thenComparing(Product::getName));
如果这定义了您产品的“自然顺序”,请考虑制作Product
工具Comparable<Product>
并在compareTo()
Product
方法中实施。
答案 3 :(得分:1)
您需要实现Comparable
接口。界面要求您添加一个名为compareTo(Product other)
的函数,在该函数中编写代码,以检查您希望对象进行比较的自定义字段。
或者你可以做@Prasad Kharkar建议的内容并写一个基本上做同样事情的Comaparator
。
http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html
http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/
答案 4 :(得分:0)
据我所知,你没有这样的方法,你可以做的是;扩展Collection的子类并添加方法进行排序(搜索或类似冒泡排序的技术等)
如果您有数据库设施(更多开销) *你可以把它放在那里并使用订单 *如果您使用JPA,只需将列表转储到实体类