Here you can find the source of sortByString(Object[] array)
Parameter | Description |
---|---|
array | The array of objects to sort |
public static void sortByString(Object[] array)
//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); } } }