arraylist排序(arraylist排序函数)
如何实现对ArrayList排序 sort
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);
ListStudent studentList = new ArrayListStudent();
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;
return s1.getAge().compareTo(s2.getAge());
// 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());
}
}
输出结果:
按年龄升序:
张三 / 11
刘武 / 19
丁晓宇 / 21
赵四 / 22
按姓名排序:
丁晓宇 / 21
刘武 / 19
张三 / 11
赵四 / 22
arraylist数组排序
用Collections工具方法
import?java.util.Collections;
import?java.util.Comparator;
//?按薪水由低到高排序
????public?void?sort()?{
????????Collections.sort(arr,?new?Comparator()?{
????????????public?int?compare(Object?o1,?Object?o2)?{
????????????????Emp?e1=?(Emp)?o1;
????????????????Emp?e2=?(Emp)?o2;
????????????????
????????????????return?Float.compare(e1.sla?,?e2.sla);
????????????}
????????});
????}
java如何对Arraylist数组进行排序(用comparable)
看代码:
import?java.util.ArrayList;
import?java.util.Arrays;
import?java.util.Collections;
public?class?Demo?{
public?static?void?main(String[]?args)?throws?Exception?{
Pair[]?pairs?=?{
new?Pair(0,?1),
new?Pair(2,?9),
new?Pair(7,?0),
new?Pair(8,?8),
new?Pair(8,?6),
new?Pair(9,?2),
new?Pair(1,?5),
new?Pair(8,?2),
new?Pair(9,?15),
new?Pair(9,?5)
};
ArrayListPair?pairList?=?new?ArrayList(Arrays.asList(pairs));
System.out.println("排序前:");
System.out.println(Arrays.toString(pairs));
Arrays.sort(pairs);?//?对数组排序
System.out.println("排序后:");
System.out.println(Arrays.toString(pairs));
System.out.println("排序前:");
System.out.println(pairList);
Collections.sort(pairList);?//?对?ArrayList?排序
System.out.println("排序后:");
System.out.println(pairList);
}
}
//?继承?Comparable?接口排序该类是?“可排序的”
//??里面的是排序时与当前实例进行比较的实例的类型
//?一般都和当前实例是同一个类型,比如这里就是?Pair?的实例?和?Pair?的实例比较
class?Pair?implements?ComparablePair?{
public?int?left;
public?int?right;
public?Pair(int?left,?int?right)?{
this.left?=?left;
this.right?=?right;
}
@Override
public?String?toString()?{
return?"["?+?left?+?",?"?+?right?+?"]";
}
//?排序规则,先按?left?排序,再按?right?排序
@Override
public?int?compareTo(Pair?that)?{
if?(this.left??that.left)?{
return?1;
}?else?if?(this.left??that.left)?{
return?-1;
}?else?if?(this.right??that.right)?{
return?1;
}?else?if?(this.right??that.right)?{
return?-1;
}
return?0;
}
}
可以发现先按 left 排序,如果 left 相等,则按 right 排序