已知一个数组的长度和内容,但是没有排序,如何将他转化为排序的数组?
我要代码
谢谢

解决方案 »

  1.   

    要这个是一个什么样的数组,像存int、double等基本数据类型的直接Arrays.sort(array)
    如果是object的,定义comparatble接口import java.util.*;
    public class Sort10262 {    /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int a[] = {1,2,3,5,4};
            double b[] = {1.1,1.2,1.4,1.3};
            String[] str = {"abc","ba","AD","aw"};
            Other[] o = {new Other(1,2),new Other(4,3),new Other(3,2)};        for(int i=0; i<a.length; i++){
                System.out.print(a[i]+"\t");
            }
            System.out.println("\nafter sort:");
            Arrays.sort(a);
            for(int i=0; i<a.length; i++){
                System.out.print(a[i]+"\t");
            }
            
            System.out.println("\n=================================");
            
            for(int i=0; i<o.length; i++){
                System.out.println(o[i]);
            }
            System.out.println("after sort:");
            Arrays.sort(o);
            for(int i=0; i<o.length; i++){
                System.out.println(o[i]);
            }
        }}
    class Other implements Comparable{
        int i,j;
        public Other(int i, int j){
            this.i = i;
            this.j = j;
        }
        public int compareTo(Object o){
            Other ot = (Other)o;
            return i<ot.i? -1 :(i==ot.i ? 0 :1);
        }
        
        public String toString(){
            return "i: "+i+"\tj: "+j;
        }
    }
      

  2.   

    给你个例子吧, int型的;
    import java.util.*;
    public class ArraySort { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
            int[] a = {3,1,34,2,8,13,11};
            Arrays.sort(a);
            System.out.println(Arrays.toString(a));
    }}如果想 其他的排列,要自己实现一下Comparator接口。sort
    public static <T> void sort(T[] a,
                                Comparator<? super T> c)
    Sorts the specified array of objects according to the order induced by the specified comparator. All elements in the array must be mutually comparable by the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the array).
    This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n*log(n) performance. 
    Parameters:
    a - the array to be sorted.
    c - the comparator to determine the order of the array. A null value indicates that the elements' natural ordering should be used. 
    Throws: 
    ClassCastException - if the array contains elements that are not mutually comparable using the specified comparator.
    See Also:
    Comparator
      

  3.   

    http://community.csdn.net/Expert/topic/4332/4332517.xml?temp=1.182193E-02