arrays类
sort(byte[] a)
对指定的 byte 型数组按数字升序进行排序。
sort(byte[] a, int fromIndex, int toIndex)
对指定 byte 型数组的指定范围按数字升序进行排序。
sort(char[] a)
对指定的 char 型数组按数字升序进行排序。
sort(char[] a, int fromIndex, int toIndex)
对指定 char 型数组的指定范围按数字升序进行排序。
sort(double[] a)
对指定的 double 型数组按数字升序进行排序。
sort(double[] a, int fromIndex, int toIndex)
对指定 double 型数组的指定范围按数字升序进行排序。
sort(float[] a)
对指定的 float 型数组按数字升序进行排序。
sort(float[] a, int fromIndex, int toIndex)
对指定 float 型数组的指定范围按数字升序进行排序。
sort(int[] a)
对指定的 int 型数组按数字升序进行排序。
sort(int[] a, int fromIndex, int toIndex)
对指定 int 型数组的指定范围按数字升序进行排序。
sort(long[] a)
对指定的 long 型数组按数字升序进行排序。
sort(long[] a, int fromIndex, int toIndex)
对指定 long 型数组的指定范围按数字升序进行排序。
sort(Object[] a)
根据元素的自然顺序,对指定对象数组按升序进行排序。
sort(Object[] a, int fromIndex, int toIndex)
根据元素的自然顺序,对指定对象数组的指定范围按升序进行排序。
sort(short[] a)
对指定的 short 型数组按数字升序进行排序。
sort(short[] a, int fromIndex, int toIndex)
对指定 short 型数组的指定范围按数字升序进行排序。
sort(T[] a, Comparator<? super T> c)
根据指定比较器产生的顺序对指定对象数组进行排序。
sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)
根据指定比较器产生的顺序对指定对象数组的指定范围进行排序。
实例1:
package com.sun;
import java.util.Arrays;
import java.util.Comparator;
/**
* 排序
* @author Administrator
*
*/
public class Px {
public static void main(String[] args) {
String[] str = {"9","6","8","2","1","5","3","4","7","10"};
Arrays.sort(str,new CustomComparator());
for(int i=0;i<str.length;i++){
System.out.print(str[i]+",");
}
}
}
/**
* 指定比较器
* @author Administrator
*
*/
class CustomComparator implements Comparator<String>{
public int compare(String str1, String str2)//重写比较方法
{
int name1 = Integer.parseInt(str1);
int name2 = Integer.parseInt(str2);
return name1 - name2;
}
}
实例2:
package com.sun;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class PxCollections {
public static void main(String[] args) {
Student stu1 = new Student (1,"zhangsan","male",28,"cs");
Student stu2 = new Student (2,"lisi","female",19,"cs");
Student stu3 = new Student (3,"wangwu","male",22,"cs");
Student stu4 = new Student (4,"zhaoliu","female",17,"cs");
Student stu5 = new Student (5,"jiaoming","male",22,"cs");
ArrayList<Student> list = new ArrayList<Student>();
list.add(stu1);
list.add(stu2);
list.add(stu3);
list.add(stu4);
list.add(stu5);
//这里就会自动根据规则进行排序
Collections.sort(list,new CustomComparator1());
for(int j=0;j<list.size();j++){
System.out.println(list.get(j)+",");
}
}
}
/**
* 指定比较器
* @author Administrator
*
*/
class CustomComparator1 implements Comparator<Student>{
public int compare(Student s1, Student s2)
{
//先排年龄
if(s1.age!=s2.age){
return s1.age-s2.age;
}else{
//年龄相同则按姓名排序
if(!s1.name.equals(s2.name)){
return s1.name.compareTo(s2.name);
}else{
//姓名也相同则按学号排序
return s1.id-s2.id;
}
}
}
}
class Student{
int age;
int id;
String gender;
String name;
String cs;
Student(int id,String name,String gender,int age,String cs){
this.age=age;
this.name=name;
this.gender=gender;
this.id=id;
this.cs=cs;
}
public String toString(){
return id+" "+name+" "+gender+" "+age+" "+cs;
}
}
转载于:https://blog.51cto.com/guohongzhi521/1260228