1.⽐较数字
List<Shoes> all_shoes = new ArrayList<Shoes>();
Collections.sort(all_shoes, new Comparator<Shoes>(){
@Override
/* *
* 返回-1表⽰:Similarity()⼤于等于Similarity(),
* 返回1表⽰:Similarity()⼩于Similarity(),
* */
public int compare(Shoes shoes1, Shoes shoes2) {
if (Similarity()&Similarity()){
return 1;
}else{
return -1;
}
}
});
System.out.println("⽐较后");
for(Shoes s:all_shoes) {
System.out.Id());
System.out.Shoes_name());
System.out.Price());
System.out.Deal());
System.out.Image());
System.out.Shop_name());
System.out.Address());
System.out.Url());
System.out.Source());
System.out.Similarity());
System.out.println("--------------------------");
}
运⾏结果(similarity按从⼤到⼩顺序排列):
2.字符串型
package dao;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
*@author chenmeiqi
*@version 2020年2⽉24⽇下午12:13:17
*/
public class test1 {
public static void main(String[] args) {
/
/ TODO Auto-generated method stub
List<String> keyList = new ArrayList<>();
keyList.add("abc");
记住我keyList.add("fgh");
keyList.add("cvb");
System.out.println("排序前:");
for(String s:keyList) {
System.out.println(s);
}
Collections.sort(keyList, new Comparator<String>(){
@Override
public int compare(String o1, String o2) {
/*
* * int compare(String o1, String o2) 返回⼀个基本类型的整型, * * 返回负数表⽰:o1 ⼩于o2,
* * 返回0 表⽰:o1和o2相等,
* * 返回正数表⽰:o1⼤于o2
* */
if (o1pareTo(o2) > 0){
return 1;
}else if (o1pareTo(o2) > 0){
return 0;
}else{
return -1;
}
}
});
System.out.println("排序后:");
for(String s:keyList) {
System.out.println(s);
}
}
}
运⾏结果:
发布评论