KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > common > Sorter


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Sorter.java
26  *
27  * Created on January 18, 2001, 3:53 PM
28  */

29
30 package com.sun.enterprise.tools.common;
31
32 /**
33  *
34  * @author administrator
35  * @version
36  */

37 // public class Sorter extends java.lang.Object {
38

39 import java.util.*;
40
41 public class Sorter
42 {
43     
44     ////////////////////////////////////////////////////////////////////////////////
45

46     public static void sort(Vector v)
47     {
48         if(v.size() <= 1)
49             return;
50
51         SorterObject[] arr = new SorterObject[v.size()];
52         Enumeration e = v.elements();
53         
54         for(int i = 0 ; e.hasMoreElements() ; i++)
55         {
56             arr[i] = new SorterObject(e.nextElement());
57         }
58
59         mergeSort(arr);
60
61         v.removeAllElements();
62
63         for(int i = 0; i < arr.length; i++)
64         {
65             v.addElement(arr[i].obj);
66         }
67     }
68     
69     ////////////////////////////////////////////////////////////////////////////////
70

71     private static void mergeSort(SorterObject[] arr)
72     {
73         int low = 0;
74         int high = arr.length;
75
76         for (int i = low; i < high; i++)
77         {
78             for (int j = i; j > low && arr[j-1].sortName.compareTo(arr[j].sortName) > 0; j--)
79             {
80                 swap(arr, j, j-1);
81             }
82         }
83     }
84     
85     ////////////////////////////////////////////////////////////////////////////////
86

87     private static void swap(SorterObject x[], int a, int b)
88     {
89         SorterObject t = x[a];
90         x[a] = x[b];
91         x[b] = t;
92     }
93     
94     ////////////////////////////////////////////////////////////////////////////////
95

96     private static void print(Vector v, String JavaDoc s)
97     {
98         System.out.println(s + "\n");//NOI18N
99

100         for(Enumeration e = v.elements(); e.hasMoreElements(); )
101         {
102             System.out.println("" + e.nextElement());//NOI18N
103
}
104     }
105     
106     ////////////////////////////////////////////////////////////////////////////////
107

108     public static void main(String JavaDoc[] args)
109     {
110         Vector v1 = new Vector();
111         //Vector v2 = new Vector();
112
//Vector v3 = new Vector();
113

114         v1.addElement("aaaa");//NOI18N
115
v1.addElement("zzzz");//NOI18N
116
v1.addElement("dddd");//NOI18N
117
v1.addElement("ccccx");//NOI18N
118
v1.addElement("cccc");//NOI18N
119
v1.addElement("cccc");//NOI18N
120
v1.addElement("cccc");//NOI18N
121
v1.addElement("bbbb");//NOI18N
122

123         print(v1, "****** before");//NOI18N
124
sort(v1);
125         print(v1, "\n******after");//NOI18N
126
}
127     
128     ////////////////////////////////////////////////////////////////////////////////
129
}
130
131 ////////////////////////////////////////////////////////////////////////////////
132

133 class SorterObject
134 {
135     SorterObject(Object JavaDoc obj)
136     {
137         this.obj = obj;
138         sortName = obj.toString();
139     }
140     String JavaDoc sortName;
141     Object JavaDoc obj;
142 }
143
Popular Tags