java中的數組與集合的排序

java中的數組與集合的排序

兩種需要排序的對象:數組和集合

1、集合

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

static voidsort(List list)
          Sorts the specified list into ascending order, according to the natural ordering of its elements.
static voidsort(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 voidsort(byte[] a)
          Sorts the specified array of bytes into ascending numerical order.
static voidsort(byte[] a, int fromIndex, int toIndex)
          Sorts the specified range of the specified array of bytes into ascending numerical order.
static voidsort(char[] a)
          Sorts the specified array of chars into ascending numerical order.
static voidsort(char[] a, int fromIndex, int toIndex)
          Sorts the specified range of the specified array of chars into ascending numerical order.
static voidsort(double[] a)
          Sorts the specified array of doubles into ascending numerical order.
static voidsort(double[] a, int fromIndex, int toIndex)
          Sorts the specified range of the specified array of doubles into ascending numerical order.
static voidsort(float[] a)
          Sorts the specified array of floats into ascending numerical order.
static voidsort(float[] a, int fromIndex, int toIndex)
          Sorts the specified range of the specified array of floats into ascending numerical order.
static voidsort(int[] a)
          Sorts the specified array of ints into ascending numerical order.
static voidsort(int[] a, int fromIndex, int toIndex)
          Sorts the specified range of the specified array of ints into ascending numerical order.
static voidsort(long[] a)
          Sorts the specified array of longs into ascending numerical order.
static voidsort(long[] a, int fromIndex, int toIndex)
          Sorts the specified range of the specified array of longs into ascending numerical order.
static voidsort(Object[] a)
          Sorts the specified array of objects into ascending order, according to the natural ordering of its elements.
static voidsort(Object[] a, Comparator c)
          Sorts the specified array of objects according to the order induced by the specified comparator.
static voidsort(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 voidsort(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 voidsort(short[] a)
          Sorts the specified array of shorts into ascending numerical order.
static voidsort(short[] a, int fromIndex, int toIndex)
          Sorts the specified range of the specified array of shorts into ascending numerical order.

如果對int[]等類型可直接升序排序
如果對Object[]類型排序,需要確定排序順序,仍舊兩種方式,被排序類本身實現Comparable接口或者提供java.util.Comparator類。

 /*
 * 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
 *
 * <b>排序方法彙總</b>
 * 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 + ">:<" + 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 --
<List.3>:<0>
<List.9>:<0>
<List.0>:<1>
<List.1>:<1>
<List.4>:<1>
<List.6>:<3>
<List.8>:<3>
<List.5>:<7>
<List.2>:<8>
<List.7>:<9>
-- sort list by comparator --
<List.7>:<5>
<List.1>:<4>
<List.2>:<3>
<List.8>:<3>
<List.3>:<2>
<List.5>:<2>
<List.9>:<2>
<List.0>:<0>
<List.4>:<0>
<List.6>:<0>
-- sort array by default --
<Array0>:<1>
<Array1>:<2>
<Array2>:<2>
<Array3>:<6>
<Array4>:<7>
<Array5>:<8>
<Array6>:<8>
<Array7>:<9>
<Array8>:<9>
<Array9>:<9>
-- sort array by comparable --
<Array2>:<0>
<Array1>:<1>
<Array7>:<1>
<Array9>:<3>
<Array0>:<4>
<Array8>:<4>
<Array5>:<5>
<Array6>:<5>
<Array3>:<8>
<Array4>:<9>
-- sort array by comparator --
<Array3>:<9>
<Array7>:<9>
<Array1>:<7>
<Array4>:<5>
<Array5>:<3>
<Array8>:<3>
<Array0>:<2>
<Array2>:<1>
<Array6>:<1>
<Array9>:<0>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章