# java中的数组与集合的排序---摘抄自:http://blog.csdn.net/jonathan_q_bo/archive/2005/11/29/539043.aspx

java中的数组与集合的排序

1、集合

java.util.Collections类提供了排序方法sort();

 static void sort(List list)            Sorts the specified list into ascending order, according to the natural ordering of its elements. static void sort(List list, Comparator c)            Sorts the specified list according to the order induced by the specified comparator.

两种排序方式：
1）被排序对象实现java.lang.Comparable接口，在其中的compareTo()方法中确定排序规则
2）用java.util.Comparator的具体实例确定排序规则

2、数组

java.util.Arrays类提供了各种重载的排序方法sort();

 static void sort(byte[] a)            Sorts the specified array of bytes into ascending numerical order. static void sort(byte[] a, int fromIndex, int toIndex)            Sorts the specified range of the specified array of bytes into ascending numerical order. static void sort(char[] a)            Sorts the specified array of chars into ascending numerical order. static void sort(char[] a, int fromIndex, int toIndex)            Sorts the specified range of the specified array of chars into ascending numerical order. static void sort(double[] a)            Sorts the specified array of doubles into ascending numerical order. static void sort(double[] a, int fromIndex, int toIndex)            Sorts the specified range of the specified array of doubles into ascending numerical order. static void sort(float[] a)            Sorts the specified array of floats into ascending numerical order. static void sort(float[] a, int fromIndex, int toIndex)            Sorts the specified range of the specified array of floats into ascending numerical order. static void sort(int[] a)            Sorts the specified array of ints into ascending numerical order. static void sort(int[] a, int fromIndex, int toIndex)            Sorts the specified range of the specified array of ints into ascending numerical order. static void sort(long[] a)            Sorts the specified array of longs into ascending numerical order. static void sort(long[] a, int fromIndex, int toIndex)            Sorts the specified range of the specified array of longs into ascending numerical order. static void sort(Object[] a)            Sorts the specified array of objects into ascending order, according to the natural ordering of its elements. static void sort(Object[] a, Comparator c)            Sorts the specified array of objects according to the order induced by the specified comparator. static void sort(Object[] a, int fromIndex, int toIndex)            Sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements. static void sort(Object[] a, int fromIndex, int toIndex, Comparator c)            Sorts the specified range of the specified array of objects according to the order induced by the specified comparator. static void sort(short[] a)            Sorts the specified array of shorts into ascending numerical order. static void sort(short[] a, int fromIndex, int toIndex)            Sorts the specified range of the specified array of shorts into ascending numerical order.

 /*  * Created on 2005-11-27  *  * TODO To change the template for this generated file go to  * Window - Preferences - Java - Code Style - Code Templates  */ package sort; import java.util.ArrayList;import java.util.Comparator; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; //import java.util.Random; import java.lang.Math; /**  * @author Jonathan  *  * 排序方法汇总  * TODO To change the template for this generated type comment go to  * Window - Preferences - Java - Code Style - Code Templates  */ public class SortCollection{  private void sortByComparableOfList(){   ArrayList al = new ArrayList();   for(int i = 0; i < 10; i++){    int tmp = (int)(Math.random()*10);    al.add(new SortedObjectOne(tmp,"List." + i));   }   Collections.sort(al);   Iterator itt = al.iterator();   while(itt.hasNext())    System.out.println(itt.next());  }    private void sortByComparatorOfList(){   ArrayList al = new ArrayList();   for(int i = 0; i < 10; i++){    int tmp = (int)(Math.random()*10);    al.add(new SortedObjectOne(tmp,"List." + i));   }   Collections.sort(al, new SortOrderAsc());   Iterator itt = al.iterator();   while(itt.hasNext())    System.out.println(itt.next());  }    /**整形数组的默认排序*/  private void sortByDefaultOfArray(){   int[] array = new int[10];   for(int i = 0; i < array.length; i++){    array[i] = (int)(Math.random()*10);   }   Arrays.sort(array);   for(int i = 0; i < array.length; i++){    System.out.println(":<" + array[i] + ">");   }  }    /**对象数组的默认排序*/  private void sortByComparableOfArray(){   SortedObjectOne[] array = new SortedObjectOne[10];   for(int i = 0; i < array.length; i++){    array[i] = new SortedObjectOne((int)(Math.random()*10), "Array" + i);   }   Arrays.sort(array);   for(int i = 0; i < array.length; i++){    System.out.println(array[i]);   }  }    /**对象数组的定制排序*/  private void sortByComparatorOfArray(){   SortedObjectOne[] array = new SortedObjectOne[10];   for(int i = 0; i < array.length; i++){    array[i] = new SortedObjectOne((int)(Math.random()*10), "Array" + i);   }   Arrays.sort(array, new SortOrderAsc());   for(int i = 0; i < array.length; i++){    System.out.println(array[i]);   }  }    public static void main(String args[]){   SortCollection sc = new SortCollection();   System.out.println("-- sort list by comparable --");   sc.sortByComparableOfList();   System.out.println("-- sort list by comparator --");   sc.sortByComparatorOfList();   System.out.println("-- sort array by default --");   sc.sortByDefaultOfArray();   System.out.println("-- sort array by comparable --");   sc.sortByComparableOfArray();   System.out.println("-- sort array by comparator --");   sc.sortByComparatorOfArray();  }   } /** 根据Comparable接口确定排序顺序 */ class SortedObjectOne implements Comparable{    public int attr1;  public String attr2;    public SortedObjectOne(int attr1,String attr2){   this.attr1 = attr1;   this.attr2 = attr2;  }    /* (non-Javadoc)   * @see java.lang.Comparable#compareTo(java.lang.Object)   */  public int compareTo(Object arg0) {   // TODO Auto-generated method stub   if(arg0 instanceof SortedObjectOne){    SortedObjectOne obj = (SortedObjectOne)arg0;    return this.attr1 - obj.attr1;   }   return 0;  }      /* (non-Javadoc)   * @see java.lang.Object#toString()   */  public String toString() {   // TODO Auto-generated method stub   return "<" + this.attr2 + ">:<" + this.attr1 + ">";  } } /** 根据Comparator确定排序顺序 */ class SortOrderAsc implements Comparator{  /* (non-Javadoc)   * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)   */  public int compare(Object arg0, Object arg1) {   // TODO Auto-generated method stub   if(arg0 instanceof SortedObjectOne && arg1 instanceof SortedObjectOne){    SortedObjectOne tmp0 = (SortedObjectOne)arg0;    SortedObjectOne tmp1 = (SortedObjectOne)arg1;    return tmp1.attr1 - tmp0.attr1;   }   return 0;  } }

 -- sort list by comparable -- :<0> :<0> :<1> :<1> :<1> :<3> :<3> :<7> :<8> :<9> -- sort list by comparator -- :<5> :<4> :<3> :<3> :<2> :<2> :<2> :<0> :<0> :<0> -- sort array by default -- :<1> :<2> :<2> :<6> :<7> :<8> :<8> :<9> :<9> :<9> -- sort array by comparable -- :<0> :<1> :<1> :<3> :<4> :<4> :<5> :<5> :<8> :<9> -- sort array by comparator -- :<9> :<9> :<7> :<5> :<3> :<3> :<2> :<1> :<1> :<0>

kenty

|
4天前
|
Java
Java排序
【2月更文挑战第7天】【2月更文挑战第17篇】List对象集合自定义排序，列出了以前的用法以及新用法。
14 0
|
5天前
|

Java泛型与集合：类型安全的集合操作实践
Java泛型与集合：类型安全的集合操作实践
8 1
|
5天前
|

Java TreeMap：基于红黑树的排序映射解析
Java TreeMap：基于红黑树的排序映射解析
10 0
|
5天前
|

Java TreeSet：基于红黑树的排序集合解析
Java TreeSet：基于红黑树的排序集合解析
14 0
|
5天前
|

Java集合框架概述：体系结构与核心接口
Java集合框架概述：体系结构与核心接口
15 0
|
5天前
|

Java中的集合框架及其应用
Java中的集合框架及其应用
12 3
|
7天前
|
Java
Java对list集合元素进行排序的几种方式
Java对list集合元素进行排序的几种方式
13 0
|
8天前
|
Java
java 实现Comparable接口排序，升序、降序、倒叙
java 实现Comparable接口排序，升序、降序、倒叙
9 0
|
11天前
|
Java

9 0
|
19天前
|
Java

21 6

• 云迁移中心

更多

更多

更多