对List集合中的元素进⾏排序⼀,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.String());
//倒叙排(从⼤到⼩)
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();
}
bigdecimal转换为integerpublic 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类:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论