当前位置:首页>开发>正文

java8里ArrayList.sort用法 如何实现对ArrayList排序 sort

2023-06-04 22:05:21 互联网 未知 开发

 java8里ArrayList.sort用法 如何实现对ArrayList排序 sort

java8里ArrayList.sort用法

JDK 6 里面,在Collections里面,静态方法

~
基本用法如
list.sort(new Comparator(){
public int compareTo(MyClass o1, MyClass o2){
return o1.compareTo(o2)
}
})

如何实现对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)
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
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())
}
}
输出结果:
按年龄升序:
张三 / 1刘武 / 1丁晓宇 / 2赵四 / 2按姓名排序:
丁晓宇 / 2刘武 / 1张三 / 1赵四 / 22

java Arrays.sort() 排序规则

首先,你要学会查文档,文档当中都写得清清楚楚。还有需要注意的是sort的排序是采用bubble的。只能在一般的处理,如果要求算法比较高的就不行了~但是他 能做的事情就很多了,比如说能给对象从小到大排序。等等。但是也有局限性的。具体的用法楼上也说了:
static void sort(byte[] a)
对指定的 byte 型数组按数字升序进行排序。
static void sort(byte[] a, int fromIndex, int toIndex)
对指定 byte 型数组的指定范围按数字升序进行排序。
static void sort(char[] a)
对指定的 char 型数组按数字升序进行排序。
static void sort(char[] a, int fromIndex, int toIndex)
对指定 char 型数组的指定范围按数字升序进行排序。
static void sort(double[] a)
对指定的 double 型数组按数字升序进行排序。
static void sort(double[] a, int fromIndex, int toIndex)
对指定 double 型数组的指定范围按数字升序进行排序。
static void sort(float[] a)
对指定的 float 型数组按数字升序进行排序。
static void sort(float[] a, int fromIndex, int toIndex)
对指定 float 型数组的指定范围按数字升序进行排序。
static void sort(int[] a)
对指定的 int 型数组按数字升序进行排序。
static void sort(int[] a, int fromIndex, int toIndex)
对指定 int 型数组的指定范围按数字升序进行排序。
static void sort(long[] a)
对指定的 long 型数组按数字升序进行排序。
static void sort(long[] a, int fromIndex, int toIndex)
对指定 long 型数组的指定范围按数字升序进行排序。
static void sort(Object[] a)
根据元素的自然顺序对指定对象数组按升序进行排序。
static void sort(Object[] a, int fromIndex, int toIndex)
根据元素的自然顺序对指定对象数组的指定范围按升序进行排序。
static void sort(short[] a)
对指定的 short 型数组按数字升序进行排序。
static void sort(short[] a, int fromIndex, int toIndex)
对指定 short 型数组的指定范围按数字升序进行排序。
static void
sort(T[] a, Comparator c)
根据指定比较器产生的顺序对指定对象数组进行排序。
static void
sort(T[] a, int fromIndex, int toIndex, Comparator c)
根据指定比较器产生的顺序对指定对象数组的指定范围进行排序。
。总之记得查文档,文档里都有

Java中Array.sort()的排序原理

sort的底层排序原理,我用自己的经验说说,不一定是你需要的,
它是对Object数组进行排序,优先级别是int,String,
int属性ComparaTo方法。而String实现String。ComparTo方法。

最新文章