public class Student {
private int age;
private String name;
private String weight;
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/**
* 按照List中的某个Int类型的属性进行排序
* @param list
*/
@SuppressWarnings("unchecked")
public static void sortIntMethod(List list){
Collections.sort(list, new Comparator(){
@Override
public int compare(Object o1, Object o2) {
Student stu1=(Student)o1;
Student stu2=(Student)o2;
if(stu1.getAge()>stu2.getAge()){
return 1;
}else if(stu1.getAge()==stu2.getAge()){
return 0;
}else{
return -1;
}
}
});
System.out.println("/////////////排序之后///////////////");
for(int i=0;i<list.size();i++){
Student st=(Student)list.get(i);
System.out.println("st.age="+st.getAge()+",st.name="+st.getName());
}
}
/**
* 按照List中的某个String类型的属性进行排序
* @param list
*/
@SuppressWarnings("unchecked")
public static void sortStringMethod(List list){
Collections.sort(list, new Comparator(){
@Override
public int compare(Object o1, Object o2) {
Student stu1=(Student)o1;
Student stu2=(Student)o2;
return stu1.getName().compareTo(stu2.getName());
}
});
System.out.println("/////////////排序之后///////////////");
for(int i=0;i<list.size();i++){
Student st=(Student)list.get(i);
System.out.println("st.age="+st.getAge()+",st.name="+st.getName());
}
}
/**
* 按照List中的某个String类型的属性进行排序
* @param list
*/
@SuppressWarnings("unchecked")
public static void sortByRuleBasedCollator(List list){
Collections.sort(list, new Comparator(){
@Override
public int compare(Object o1, Object o2) {
return ((java.text.RuleBasedCollator)java.text.Collator.getInstance(java.util.Locale.CHINA)).compare(((Student)o1).getName(), ((Student)o2).getName());
}
});
System.out.println("/////////////排序之后///////////////");
for(int i=0;i<list.size();i++){
Student st=(Student)list.get(i);
System.out.println("st.age="+st.getAge()+",st.name="+st.getName());
}
}
@SuppressWarnings("unchecked")
public static void main(String[] args) {
ArrayList list=new ArrayList();
Student t1=new Student();
t1.setAge(35);
t1.setName("wanglei");
list.add(t1);
Student t2=new Student();
t2.setAge(4);
t2.setName("lisi");
list.add(t2);
Student t3=new Student();
t3.setAge(56);
t3.setName("zhonghua");
list.add(t3);
Student t4=new Student();
t4.setAge(39);
t4.setName("waanglei");
list.add(t4);
System.out.println("/////////////排序之前///////////////");
for(int i=0;i<list.size();i++){
Student st=(Student)list.get(i);
System.out.println("st.age="+st.getAge()+",st.name="+st.getName());
}
//按照List中的某个Int类型的属性进行排序
sortIntMethod(list);
//按照List中的某个String类型的属性进行排序
sortStringMethod(list);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2026 源码网商城 (www.ymwmall.com) 版权所有