# Sorting a Vector

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

###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Author Commented:
Edited text of question.
0
Commented:
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

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

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

Ganesh
0
Commented:
It is simply an interface you define. In the example I have it defined as:

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

0
Commented:
Is there something else you needed clarified before you graded this question?
0
Author Commented: