您的位置 首页 网络技术

java arraylist排序

package com.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Test {
public static void main(String[] args) {
Student zlj = new Student(“丁晓宇”, 21);
Student dxy = new Student(“赵四”, 22);
Student cjc = new Student(“张三”, 11);
Student lgc = new Student(“刘武”, 19);
List studentList = new ArrayList();
studentList.add(zlj);
studentList.add(dxy);
studentList.add(cjc);
studentList.add(lgc);
System.out.println(“按年龄升序:”);
Collections.sort(studentList, new SortByAge());
for (Student student : studentList) {
System.out.println(student.getName() + ” / ” + student.getAge());
}
System.out.println();
System.out.println(“按姓名排序:”);
Collections.sort(studentList, new SortByName());
for (Student student : studentList) {
System.out.println(student.getName() + ” / ” + student.getAge());
}
}
}
class SortByAge implements Comparator {
public int compare(Object o1, Object o2) {
Student s1 = (Student) o1;
Student s2 = (Student) o2;
if (s1.getAge() > s2.getAge())
return 1;
return -1;
}
}
class SortByName implements Comparator {
public int compare(Object o1, Object o2) {
Student s1 = (Student) o1;
Student s2 = (Student) o2;
return s1.getName().compareTo(s2.getName());
}
}

猫叔总结了 适合新手操作的副业 《淘宝虚拟产品月入2万的 6个 细分类目》的电子书 仅供参考

如果你对虚拟产品比较感兴趣,可以点击:

淘宝卖什么虚拟产品赚钱(月入2万+)

hadoopall

关于花猫大叔短视频创业 作者: hadoopall

热门文章

发表评论

电子邮件地址不会被公开。