1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core.util;
14 * The SortOperation takes a collection of objects and returns a sorted
15 * collection of these objects. The sorting of these objects is based on their
16 * toString(). They are sorted in alphabetical order.
18 public class ToStringSorter {
19 Object[] sortedObjects;
21 String[] sortedStrings;
24 * Returns true if stringTwo is 'greater than' stringOne This is the
25 * 'ordering' method of the sort operation.
27 public boolean compare(String stringOne, String stringTwo) {
28 return stringOne.compareTo(stringTwo) < 0;
32 * Sort the objects in sorted collection and return that collection.
34 private void quickSort(int left, int right) {
35 int originalLeft = left;
36 int originalRight = right;
37 int midIndex = (left + right) / 2;
38 String midToString = this.sortedStrings[midIndex];
41 while (compare(this.sortedStrings[left], midToString))
43 while (compare(midToString, this.sortedStrings[right]))
46 Object tmp = this.sortedObjects[left];
47 this.sortedObjects[left] = this.sortedObjects[right];
48 this.sortedObjects[right] = tmp;
49 String tmpToString = this.sortedStrings[left];
50 this.sortedStrings[left] = this.sortedStrings[right];
51 this.sortedStrings[right] = tmpToString;
55 } while (left <= right);
57 if (originalLeft < right)
58 quickSort(originalLeft, right);
59 if (left < originalRight)
60 quickSort(left, originalRight);
64 * Return a new sorted collection from this unsorted collection. Sort using
67 public void sort(Object[] unSortedObjects, String[] unsortedStrings) {
68 int size = unSortedObjects.length;
69 this.sortedObjects = new Object[size];
70 this.sortedStrings = new String[size];
72 // copy the array so can return a new sorted collection
73 System.arraycopy(unSortedObjects, 0, this.sortedObjects, 0, size);
74 System.arraycopy(unsortedStrings, 0, this.sortedStrings, 0, size);
76 quickSort(0, size - 1);