[Webinar] Streamline your web hosting managementRegister Today

x
Solved

# Sorting a Vector

Posted on 2000-01-13
Medium Priority
242 Views
I have a Vector which consists of objects of SimpleClass.  SimpleClass you can find below.  I want to sort the Vector on any of the values in the SimpleClass and re-arrange the Vector in that order.  I will really appreciate, if anybody can help me with code.

public class SimpleClass
{
public int code;
public String Name;
public String TelNumber;
}

Ganesh

0
Question by:ganown
• 4
• 3

Author Comment

ID: 2350421
Edited text of question.
0

LVL 16

Accepted Solution

ID: 2350473
Here is a quick sort for a Vector. The only requirement it has is that the objects implement a Sortable interface. The Sortable interface provides a comp method that returns an -1,0 or 1 indicating with an element is less than, equal or greater than another:

public static void qsort(Vector s)
{      sv=s;
quicksort(0,s.size());
return;
}

private static void quicksort(int start,int end)
{      Sortable iobj;

int last=end-1;
int i=start+1;
if(i<=last)
{      Sortable ptr=(Sortable)sv.elementAt(start);
while(i<=last)
{      iobj=(Sortable)sv.elementAt(i);
if(ptr.comp(iobj)>0)
{      sv.setElementAt(iobj,i-1);
++i;
}
else
{      iobj=(Sortable)sv.elementAt(last);
sv.setElementAt(sv.elementAt(i),last);
sv.setElementAt(iobj,i);
--last;
}
}
sv.setElementAt(ptr,i-1);
quicksort(start,i-1);
quicksort(i,end);
}
return;
}

SimpleClass would have to become something like:

public class SimpleClass implements Sortable
{
public int code;
public String Name;
public String TelNumber;

public int comp(Sortable o)
{   SimpleClass sc=(SimpleClass)o;
if(code!=sc.code)return(code<sc.code?-1:1);
return(0);
}
}

0

Author Comment

ID: 2350527
Sorry, I am not clear with which methods are under the Sortable interface.  I will appreciate if you explain it.

Ganesh
0

LVL 16

Expert Comment

ID: 2350646
It is simply an interface you define. In the example I have it defined as:

public interface Sortable
{      public int comp(Sortable o);
}

0

LVL 16

Expert Comment

ID: 2389168
Is there something else you needed clarified before you graded this question?
0

Author Comment

ID: 2389748

0

LVL 16

Expert Comment

ID: 2393266
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
###### Suggested Courses
Course of the Month9 days, 7 hours left to enroll