Java Array Sort sortByString(Object[] array)

Here you can find the source of sortByString(Object[] array)

Description

Performs a quicksort of the given objects by their string representation in ascending order.

License

Open Source License

Parameter

Parameter Description
array The array of objects to sort

Declaration

public static void sortByString(Object[] array) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2003, 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*  w  w w  .  ja v  a2s .c om*/
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Performs a quicksort of the given objects
     * by their string representation in ascending order.
     * <p> 
     *
     * @param array   The array of objects to sort
     */
    public static void sortByString(Object[] array) {
        qSortByString(array, 0, array.length - 1);
    }

    /**
     * Sorts the array of objects by their string representation
     * in ascending order.
     * <p>
     * This is a version of C.A.R Hoare's Quick Sort algorithm.
     *
     * @param array   the   array of objects to sort
     * @param start   the start index to begin sorting
     * @param stop      the end index to stop sorting
     * 
     * @exception   ArrayIndexOutOfBoundsException when <code>start < 0</code>
     *            or <code>end >= array.length</code>
     */
    public static void qSortByString(Object[] array, int start, int stop) {
        if (start >= stop)
            return;

        int left = start; // left index
        int right = stop; // right index
        Object temp; // for swapping

        // arbitrarily establish a partition element as the midpoint of the array
        String mid = String.valueOf(array[(start + stop) / 2]);

        // loop through the array until indices cross
        while (left <= right) {
            // find the first element that is smaller than the partition element from the left
            while ((left < stop) && (String.valueOf(array[left]).compareTo(mid) < 0)) {
                ++left;
            }
            // find an element that is smaller than the partition element from the right
            while ((right > start) && (mid.compareTo(String.valueOf(array[right])) < 0)) {
                --right;
            }
            // if the indices have not crossed, swap
            if (left <= right) {
                temp = array[left];
                array[left] = array[right];
                array[right] = temp;
                ++left;
                --right;
            }
        }
        // sort the left partition, if the right index has not reached the left side of array
        if (start < right) {
            qSortByString(array, start, right);
        }
        // sort the right partition, if the left index has not reached the right side of array
        if (left < stop) {
            qSortByString(array, left, stop);
        }
    }
}

Related

  1. sortByIndex(final double[] data, int size)
  2. sortByIndex(int start, int end, int[] indexes, double[] values)
  3. sortByIndex(int start, int end, int[] indexes, double[] values)
  4. sortByLength(String[] proposals)
  5. sortByLengthDesc(String[] ss)
  6. sortByValue(int start, int end, double[] values, int[] indexes)
  7. sortByValueStable(int start, int end, double[] values, int[] indexes)
  8. sortCompare(String[] valuesOne, String[] valuesTwo)
  9. sortDesc(long[] keys, int[] values)