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;
}


Thanks In Advance,
Ganesh

ganownAsked:
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.

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

Your issues matter to us.

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

Start your 7-day free trial
ganownAuthor Commented:
Sorry, I am not clear with which methods are under the Sortable interface.  I will appreciate if you explain it.

Thanks In Advance
Ganesh
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

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

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

0
imladrisCommented:
Is there something else you needed clarified before you graded this question?
0
ganownAuthor Commented:
I have already Graded and given you the points of 25.

0
imladrisCommented:
I appreciate the intention. However, the way it works is you have put up a question for which your points account would be deducted 25 points. I have proposed an answer. This moves it to the locked questions section. If you had not been able to use the answer you could have rejected the answer, and hope for a more useful one. However, that has not been the case, and the alternative is that you now approve of the answer by grading it. This will move it to the Previously Asked Question section. Note that the grade you give does not affect the amount of points deducted from your account. That is a fixed 25 regardless of grade you select.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.