Java Array Sort sort(Object[] array_, boolean accendingOrder_)

Here you can find the source of sort(Object[] array_, boolean accendingOrder_)

Description

sort

License

Open Source License

Declaration

public static void sort(Object[] array_, boolean accendingOrder_) 

Method Source Code


//package com.java2s;

import java.util.*;

public class Main {
    public static void sort(Object[] array_, boolean accendingOrder_) {
        // bubble sort
        String[] ids_ = new String[array_.length];
        for (int i = 0; i < array_.length; i++)
            ids_[i] = array_[i].toString();
        for (;;) {
            boolean changed_ = false;
            for (int i = 0; i < array_.length - 1; i++) {
                String s = ids_[i];
                String t = ids_[i + 1];
                int result_ = compare(s, t);
                if (result_ == 0)
                    continue;
                if (!((result_ > 0) ^ accendingOrder_)) {
                    //exchange
                    Object tmp_ = array_[i];
                    array_[i] = array_[i + 1];
                    array_[i + 1] = tmp_;
                    ids_[i] = t;/*from w  w w .  j  a v a 2  s. c o  m*/
                    ids_[i + 1] = s;
                    changed_ = true;
                }
            }
            if (!changed_)
                break;
        }
    }

    public static void sort(String[] array_, boolean accendingOrder_) {
        // bubble sort
        for (;;) {
            boolean changed_ = false;
            for (int i = 0; i < array_.length - 1; i++) {
                String s = array_[i];
                String t = array_[i + 1];
                int result_ = compare(s, t);
                if (result_ == 0)
                    continue;
                if (!((result_ > 0) ^ accendingOrder_)) {
                    //exchange
                    array_[i] = t;
                    array_[i + 1] = s;
                    changed_ = true;
                }
            }
            if (!changed_)
                break;
        }
    }

    public static void sort(StringBuffer[] array_, boolean accendingOrder_) {
        // bubble sort
        for (;;) {
            boolean changed_ = false;
            for (int i = 0; i < array_.length - 1; i++) {
                StringBuffer s = array_[i];
                StringBuffer t = array_[i + 1];
                int result_ = compare(s, t);
                if (result_ == 0)
                    continue;
                if (!((result_ > 0) ^ accendingOrder_)) {
                    //exchange
                    array_[i] = t;
                    array_[i + 1] = s;
                    changed_ = true;
                }
            }
            if (!changed_)
                break;
        }
    }

    /**
     * Format a double number.
     * @param  f  # of digits in fraction
     */
    public static String toString(double d, int f) {
        return toString(d, -1, f);
    }

    /**
     * Format a double number.
     * @param  n  length of result
     * @param  f  # of digits in fraction
     */
    public static String toString(double d, int nn, int ff) {
        String r_ = d + "";
        int ii_ = r_.indexOf(".");
        String head_ = null, tail_ = null;

        if (ii_ < 0) {
            head_ = r_;
            tail_ = "";
        } else {
            head_ = r_.substring(0, ii_);
            tail_ = r_.substring(ii_ + 1);
        }

        int dot = ff > 0 ? 1 : 0;

        if (nn < 0)
            nn = head_.length() + dot + ff;

        if (head_.length() > nn)
            return head_;
        else if (head_.length() + ff + dot < nn) // prepends " " to head_
        {
            StringBuffer sb_ = new StringBuffer(nn - ff - dot - head_.length());
            for (int i = 0; i < sb_.capacity(); i++)
                sb_.append(' ');
            head_ = sb_ + head_;
        }

        if (ff == 0)
            return head_;

        if (tail_.length() > ff) // truncate tail_
        {
            tail_ = tail_.substring(0, ff);
        } else // appends "0" to tail_
        {
            StringBuffer sb_ = new StringBuffer(ff - tail_.length());
            for (int i = 0; i < sb_.capacity(); i++)
                sb_.append('0');
            tail_ += sb_.toString();
        }

        return head_ + "." + tail_;
        /*
        if (d == 0) return "0";
            
        double ten = Math.exp(Math.log(10)*ff);
        String sign = "";
        if (d < 0) { d = -d;  sign = "-"; }
            
        //System.out.println("double = " + d);
        //System.out.println("  sign = " + sign);
        //System.out.println("   ten = " + ten);
            
        if (d >= 1) 
        {
          long v = (long) (d*ten) / (long)ten;
          long f = (long) (d*ten) % (long)ten;
            
          return sign + v + "." + f;
        }
        else
        {
          double log = Math.log(d)/Math.log(10);
          int e = (int)log;
          if (log - e < 0) e--;
          log -= e;
          double fraction = Math.exp(Math.log(10)*log);
            
          //System.out.println("        log = " + log);
          //System.out.println("   fraction = " + fraction);
          //System.out.println("exponential = " + e);
          return sign + toString(fraction, n) + 
        (e==0? "": "e" + (e>0? "+":"") + e);
        }
        */
    }

    public static String toString(Hashtable ht_) {
        StringBuffer sb_ = new StringBuffer();
        Enumeration keys_ = ht_.keys(), elements_ = ht_.elements();
        while (keys_.hasMoreElements()) {
            sb_.append(toString(keys_.nextElement()) + ": " + toString(elements_.nextElement()) + "\n");
        }
        return sb_.toString();
    }

    public static String toString(Object o_) {
        return toString(o_, ",", 10);
    }

    public static String toString(Object o_, String separator_, int maxcount_) {
        if (o_ instanceof long[])
            return toString((long[]) o_, separator_, maxcount_);
        if (o_ instanceof int[])
            return toString((int[]) o_, separator_, maxcount_);
        if (o_ instanceof boolean[])
            return toString((boolean[]) o_, separator_, maxcount_);
        if (o_ instanceof double[])
            return toString((double[]) o_, separator_, maxcount_);
        if (o_ instanceof float[])
            return toString((float[]) o_, separator_, maxcount_);
        if (o_ instanceof byte[])
            return toString((byte[]) o_, separator_, maxcount_);
        if (o_ instanceof char[])
            return toString((char[]) o_, separator_, maxcount_);
        if (o_ == null)
            return "<null>";
        if (!(o_ instanceof Object[]))
            return o_.toString();
        Object[] a_ = (Object[]) o_;
        if (a_.length == 0)
            return "Object[0]";
        StringBuffer sb_ = new StringBuffer("(" + toString(a_[0]));
        maxcount_ = Math.min(maxcount_, a_.length);
        for (int i = 1; i < maxcount_; i++)
            sb_.append(separator_ + toString(a_[i]));
        if (a_.length > maxcount_)
            sb_.append(separator_ + "...");
        sb_.append(")");
        return sb_.toString();
    }

    public static String toString(double[] a_) {
        return toString(a_, ",", 10);
    }

    public static String toString(double[] a_, String separator_, int maxcount_) {
        if (a_ == null)
            return "<null_double[]>";
        if (a_.length == 0)
            return "double[0]";
        StringBuffer sb_ = new StringBuffer("(" + a_[0]);
        maxcount_ = Math.min(maxcount_, a_.length);
        for (int i = 1; i < maxcount_; i++)
            sb_.append(separator_ + a_[i]);
        if (a_.length > maxcount_)
            sb_.append(separator_ + "...");
        sb_.append(")");
        return sb_.toString();
    }

    public static String toString(float[] a_) {
        return toString(a_, ",", 10);
    }

    public static String toString(float[] a_, String separator_, int maxcount_) {
        if (a_ == null)
            return "<null_float[]>";
        if (a_.length == 0)
            return "float[0]";
        StringBuffer sb_ = new StringBuffer("(" + a_[0]);
        maxcount_ = Math.min(maxcount_, a_.length);
        for (int i = 1; i < maxcount_; i++)
            sb_.append(separator_ + a_[i]);
        if (a_.length > maxcount_)
            sb_.append(separator_ + "...");
        sb_.append(")");
        return sb_.toString();
    }

    public static String toString(long[] a_) {
        return toString(a_, ",", 10);
    }

    public static String toString(long[] a_, String separator_, int maxcount_) {
        if (a_ == null)
            return "<null_long[]>";
        if (a_.length == 0)
            return "long[0]";
        StringBuffer sb_ = new StringBuffer("(" + a_[0]);
        for (int i = 1; i < a_.length; i++)
            sb_.append(separator_ + a_[i]);
        if (a_.length > maxcount_)
            sb_.append(separator_ + "...");
        sb_.append(")");
        return sb_.toString();
    }

    public static String toString(int[] a_) {
        return toString(a_, ",", 10);
    }

    public static String toString(int[] a_, String separator_, int maxcount_) {
        if (a_ == null)
            return "<null_int[]>";
        if (a_.length == 0)
            return "int[0]";
        StringBuffer sb_ = new StringBuffer("(" + a_[0]);
        maxcount_ = Math.min(maxcount_, a_.length);
        for (int i = 1; i < maxcount_; i++)
            sb_.append(separator_ + a_[i]);
        if (a_.length > maxcount_)
            sb_.append(separator_ + "...");
        sb_.append(")");
        return sb_.toString();
    }

    public static String toString(boolean[] a_) {
        return toString(a_, ",", 10);
    }

    public static String toString(boolean[] a_, String separator_, int maxcount_) {
        if (a_ == null)
            return "<null_boolean[]>";
        if (a_.length == 0)
            return "boolean[0]";
        StringBuffer sb_ = new StringBuffer("(" + a_[0]);
        maxcount_ = Math.min(maxcount_, a_.length);
        for (int i = 1; i < maxcount_; i++)
            sb_.append(separator_ + a_[i]);
        if (a_.length > maxcount_)
            sb_.append(separator_ + "...");
        sb_.append(")");
        return sb_.toString();
    }

    public static String toString(byte[] a_) {
        return toString(a_, ",", 10);
    }

    public static String toString(byte[] a_, String separator_, int maxcount_) {
        if (a_ == null)
            return "<null_byte[]>";
        if (a_.length == 0)
            return "byte[0]";
        StringBuffer sb_ = new StringBuffer("(" + ((int) a_[0] & 0x0FF));
        maxcount_ = Math.min(maxcount_, a_.length);
        for (int i = 1; i < maxcount_; i++)
            sb_.append(separator_ + ((int) a_[i] & 0x0FF));
        if (a_.length > maxcount_)
            sb_.append(separator_ + "...");
        sb_.append(")");
        return sb_.toString();
    }

    public static String toString(char[] a_) {
        return toString(a_, ",", 10);
    }

    public static String toString(char[] a_, String separator_, int maxcount_) {
        if (a_ == null)
            return "<null_char[]>";
        if (a_.length == 0)
            return "char[0]";
        StringBuffer sb_ = new StringBuffer("(" + ((int) a_[0] & 0x0FFFF));
        maxcount_ = Math.min(maxcount_, a_.length);
        for (int i = 1; i < maxcount_; i++)
            sb_.append(separator_ + ((int) a_[i] & 0x0FFFF));
        if (a_.length > maxcount_)
            sb_.append(separator_ + "...");
        sb_.append(")");
        return sb_.toString();
    }

    public static String toString(Class class_) {
        if (class_ == int[].class)
            return "int[]";
        else if (class_ == double[].class)
            return "double[]";
        else if (class_ == long[].class)
            return "long[]";
        else if (class_ == boolean[].class)
            return "boolean[]";
        else if (class_ == byte[].class)
            return "byte[]";
        else if (class_ == float[].class)
            return "float[]";
        else
            return class_.getName();
    }

    /**
     * Returns 1 if s &gt; t, -1 if s < t, 0 otherwise.
     */
    public static int compare(String s, String t) {
        int slen_ = s.length();
        int tlen_ = t.length();
        int len_ = Math.min(slen_, tlen_);
        for (int i = 0; i < len_; i++) {
            char a = s.charAt(i), b = t.charAt(i);
            if (a > b)
                return 1;
            else if (a < b)
                return -1;
        }
        if (slen_ > tlen_)
            return 1;
        else if (slen_ < tlen_)
            return -1;
        else
            return 0;
    }

    /**
     * Returns 1 if s &gt; t, -1 if s < t, 0 otherwise.
     */
    public static int compare(StringBuffer s, StringBuffer t) {
        int slen_ = s.length();
        int tlen_ = t.length();
        int len_ = Math.min(slen_, tlen_);
        for (int i = 0; i < len_; i++) {
            char a = s.charAt(i), b = t.charAt(i);
            if (a > b)
                return 1;
            else if (a < b)
                return -1;
        }
        if (slen_ > tlen_)
            return 1;
        else if (slen_ < tlen_)
            return -1;
        else
            return 0;
    }
}

Related

  1. sort(Object[] a)
  2. sort(Object[] arr, int start, int end)
  3. sort(Object[] array)
  4. sort(Object[] array, Comparator comparator)
  5. sort(Object[] array, int start, int end)
  6. sort(String a[])
  7. sort(String[] colnos)
  8. sort(String[] s)
  9. sort(String[] sArray)