如何给List集合排序
⼀,List<Integer>的排序
⽰例代码:
List<Integer> list = new ArrayList<Integer>();
list.add(6);
bigdecimal转换为integerlist.add(8);
list.add(4);
list.add(5);
//正序排(从⼩到⼤)
Collections.sort(list);
System.out.String());
/
/倒叙排(注意:倒叙的话就是按照添加的顺序进⾏倒叙!可查看⾃测结果,字符串和数字都是如此) verse(list);
System.out.String());
(⾃测结果)
运⾏结果:
正序排(从⼩到⼤):[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("]");
String();
}
@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.String());
}
//按照数量从⼤到⼩排序
productList.verseOrder()); System.out.println("按照数量从⼤到⼩排序:");
for (Product product : productList) {
System.out.String());
}
运⾏结果:
按照数量从⼩到⼤排序:
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("]");
String();
}
}
测试代码:
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) {
Num()>=o2.getNum()) {
return 1;
}
return -1;
}
});
System.out.println("按照数量从⼩到⼤排序:");
for (Product product : productList) {
System.out.String());
}
//按照数量从⼤到⼩排序
Collections.sort(productList, verseOrder(new Comparator<Product>() { @Override
public int compare(Product o1, Product o2) {
Num()>=o2.getNum()) {
return 1;
}
return -1;
}
}));
System.out.println("按照数量从⼤到⼩排序:");
for (Product product : productList) {
System.out.String());
}
运⾏结果:
按照数量从⼩到⼤排序:
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("]");
String();
}
}
测试代码:
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) {
Price()Price())==0) {
Num()>=o2.getNum()) {
return 1;
}
return -1;
}
Price()Price());
}
});
for (Product product : productList) {
System.out.String());
}
运⾏结果:
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]
---------------------
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论