一,List<Integer>的排序
示例代码:
List<Integer> list = new ArrayList<Integer>();
list.add(6);
list.add(8);
list.add(4);
list.add(5);
//正序排(从小到大)
Collections.sort(list);
System.out.println(list.toString());
//倒叙排 (注意:倒叙的话就是按照添加的顺序进行倒叙! 可查看自测结果,字符串和数字都是如此)
Collections.reverse(list);
System.out.println(list.toString());
(自测结果)
运行结果:
正序排(从小到大):[4, 5, 6, 8]
倒叙排(从大到小):[8, 6, 5, 4]
二,对类中单个字段进行排序
1.类实现Comparable接口的compareTo方法
定义product类:
public class Product implements Comparable<Product>{
/**
* ID
*/
private Integer id;
/**
* 数量
*/
private Integer num;
public Product() {
super();
}
public Product(Integer id, Integer num) {
super();
this.id = id;
this.num = num;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(“Product [id=”);
builder.append(id);
builder.append(“, num=”);
builder.append(num);
builder.append(“]”);
return builder.toString();
}
@Override
public int compareTo(Product o) {
if(this.num>=o.getNum()) {
return 1;
}
return -1;
}
}
测试代码:
List<Product> productList = new ArrayList<Product>();
productList.add(new Product(1, 50));
productList.add(new Product(1, 30));
productList.add(new Product(1, 10));
productList.add(new Product(1, 55));
productList.add(new Product(1, 70));
//按照数量从小到大排序
productList.sort(Comparator.naturalOrder());
System.out.println(“按照数量从小到大排序:”);
for (Product product : productList) {
System.out.println(product.toString());
}
//按照数量从大到小排序
productList.sort(Comparator.reverseOrder());
System.out.println(“按照数量从大到小排序:”);
for (Product product : productList) {
System.out.println(product.toString());
}
运行结果:
按照数量从小到大排序:
Product [id=1, num=10]
Product [id=1, num=30]
Product [id=1, num=50]
Product [id=1, num=55]
Product [id=1, num=70]
按照数量从大到小排序:
Product [id=1, num=70]
Product [id=1, num=55]
Product [id=1, num=50]
Product [id=1, num=30]
Product [id=1, num=10]
2.使用Comparator类进行排序
定义Product类:
public class Product{
/**
* ID
*/
private Integer id;
/**
* 数量
*/
private Integer num;
public Product() {
super();
}
public Product(Integer id, Integer num) {
super();
this.id = id;
this.num = num;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(“Product [id=”);
builder.append(id);
builder.append(“, num=”);
builder.append(num);
builder.append(“]”);
return builder.toString();
}
}
测试代码:
List<Product> productList = new ArrayList<Product>();
productList.add(new Product(1, 50));
productList.add(new Product(1, 30));
productList.add(new Product(1, 10));
productList.add(new Product(1, 55));
productList.add(new Product(1, 70));
//按照数量从小到大排序
Collections.sort(productList, new Comparator<Product>() {
@Override
public int compare(Product o1, Product o2) {
if(o1.getNum()>=o2.getNum()) {
return 1;
}
return -1;
}
});
System.out.println(“按照数量从小到大排序:”);
for (Product product : productList) {
System.out.println(product.toString());
}
//按照数量从大到小排序
Collections.sort(productList, Collections.reverseOrder(new Comparator<Product>() {
@Override
public int compare(Product o1, Product o2) {
if(o1.getNum()>=o2.getNum()) {
return 1;
}
return -1;
}
}));
System.out.println(“按照数量从大到小排序:”);
for (Product product : productList) {
System.out.println(product.toString());
}
运行结果:
按照数量从小到大排序:
Product [id=1, num=10]
Product [id=1, num=30]
Product [id=1, num=50]
Product [id=1, num=55]
Product [id=1, num=70]
按照数量从大到小排序:
Product [id=1, num=70]
Product [id=1, num=55]
Product [id=1, num=50]
Product [id=1, num=30]
Product [id=1, num=10]
三,对类中多个字段进行排序
定义Product类:
import java.math.BigDecimal;
public class Product{
/**
* ID
*/
private Integer id;
/**
* 价格
*/
private BigDecimal price;
/**
* 数量
*/
private Integer num;
public Product() {
super();
}
public Product(Integer id, BigDecimal price, Integer num) {
super();
this.id = id;
this.price = price;
this.num = num;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(“Product [id=”);
builder.append(id);
builder.append(“, price=”);
builder.append(price);
builder.append(“, num=”);
builder.append(num);
builder.append(“]”);
return builder.toString();
}
}
测试代码:
List<Product> productList = new ArrayList<Product>();
productList.add(new Product(1, new BigDecimal(60), 50));
productList.add(new Product(2, new BigDecimal(70), 30));
productList.add(new Product(3, new BigDecimal(80), 10));
productList.add(new Product(4, new BigDecimal(75), 55));
productList.add(new Product(5, new BigDecimal(30), 70));
productList.add(new Product(5, new BigDecimal(75), 60));
//按照价格从小到大排序,如果价格相等,则按照剩余数量从小到大排序
Collections.sort(productList,new Comparator<Product>() {
@Override
public int compare(Product o1, Product o2) {
if(o1.getPrice().compareTo(o2.getPrice())==0) {
if(o1.getNum()>=o2.getNum()) {
return 1;
}
return -1;
}
return o1.getPrice().compareTo(o2.getPrice());
}
});
for (Product product : productList) {
System.out.println(product.toString());
}
运行结果:
Product [id=5, price=30, num=70]
Product [id=1, price=60, num=50]
Product [id=2, price=70, num=30]
Product [id=4, price=75, num=55]
Product [id=5, price=75, num=60]
Product [id=3, price=80, num=10]
———————