[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Sorting a Vector

Posted on 2000-01-13
7
Medium Priority
?
242 Views
Last Modified: 2010-04-01
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

0
Comment
Question by:ganown
  • 4
  • 3
7 Comments
 

Author Comment

by:ganown
ID: 2350421
Edited text of question.
0
 
LVL 16

Accepted Solution

by:
imladris earned 40 total points
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

by:ganown
ID: 2350527
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 16

Expert Comment

by:imladris
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

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

Author Comment

by:ganown
ID: 2389748
I have already Graded and given you the points of 25.

0
 
LVL 16

Expert Comment

by:imladris
ID: 2393266
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question