Java Array Sort sortStringArray(String array[], boolean inplace)

Here you can find the source of sortStringArray(String array[], boolean inplace)

Description

Sort the given String array in place.

License

Open Source License

Parameter

Parameter Description
array The array of String to sort.
inplace Sort the array in place if <strong>true</strong>, allocate a fresh array for the result otherwise.

Return

The same array, with string sorted.

Declaration

public static String[] sortStringArray(String array[], boolean inplace) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from  w  w w . ja  v a2 s.c o  m
     * Sort the given String array in place.
     * @param array The array of String to sort.
     * @param inplace Sort the array in place if <strong>true</strong>, 
     *    allocate a fresh array for the result otherwise.
     * @return The same array, with string sorted.
     */
    public static String[] sortStringArray(String array[], boolean inplace) {
        String tosort[] = array;
        if (!inplace) {
            tosort = new String[array.length];
            System.arraycopy(array, 0, tosort, 0, array.length);
        }
        quickSortStringArray(tosort, 0, tosort.length - 1);
        return tosort;
    }

    /**
     * Quick sort the given chunk of the array in place.
     * @param array The array to sort.
     * @param lo0 The low bound of the chunk of the array to sort.
     * @param hi0 The high bound of the array to sort.
     */
    static void quickSortStringArray(String array[], int lo0, int hi0) {
        int lo = lo0;
        int hi = hi0;
        String mid = null;

        if (hi0 > lo0) {
            mid = array[(lo0 + hi0) / 2];
            while (lo <= hi) {
                while ((lo < hi0) && (array[lo].compareTo(mid) < 0))
                    ++lo;
                while ((hi > lo0) && (array[hi].compareTo(mid) > 0))
                    --hi;
                if (lo <= hi) {
                    String tmp = array[lo];
                    array[lo] = array[hi];
                    array[hi] = tmp;
                    ++lo;
                    --hi;
                }
            }
            if (lo0 < hi)
                quickSortStringArray(array, lo0, hi);
            if (lo < hi0)
                quickSortStringArray(array, lo, hi0);
        }
    }
}

Related

  1. sortPermutation(final int[] A)
  2. sortPixels(double[] data, int numPixel)
  3. sortPopup(Integer[] integers)
  4. sortRetain(long[] a)
  5. sortReverseOrder(String[] strings)
  6. sortStringArray(String[] array)
  7. sortStringArray(String[] array)
  8. sortStringArray(String[] source)
  9. sortStringArrayAlphabetically(String[] strings)