Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comariing and Sorting Vector Objects...by name

Status
Not open for further replies.

fixthebug2003

Programmer
Oct 20, 2003
294
US
Hi,
I have a Vector Object, that has a Name Variable of type String. I need to sort the vector objects in the order of Name,Alphabetically. If the Name field has null value then that should appear first.

can someone tell me how to do this?

Thanks
Fixthebug2003
 
Well, I can think on a few ways.

1.- You can do it yourself by implementing any of the well-known sorting algorithms (bubble, quicksort ...), remembering that a null String is higher that a non-null one.

2.- You can use sort method at Arrays class by defining a Comparator that remembers the null thingie

3.- You can do a first step, removien the null values from the Vector and placing them on another on at first place and then use Arrays class with default Comparator for Strings.

Cheers.

Dian
 
Solution #2 is my suggestion as well.

Implement Comparable on your vector object and add a CompareTo method like this:

Code:
public class VectorObject implements Comparable
{
   String Name = null;
   
   public int compareTo( Object o )
   {
      if (this.Name == null)  return -1;
      if (((VectorObject)o).Name == null)  return 1;
      return this.Name.compareTo(((VectorObject)o).Name);
   }
}

Then just sort the Vector with Collections.sort().
 
Arghh, that's it, idarke.

Forget about Arrays class, I was thinking on arrays, not Vectors.

Cheers.

Dian
 
Arghh, that's it, idarke.

Forget about Arrays class, I was thinking on arrays, not Vectors.

Cheers.

Dian
 
well you'd want to return 0 if they were both equal to null, but yeah, a comparator is the way to go. Just put "if (this.name == o) return 0;" in there at the top.<BR><BR>If you have control over which collection type to use, and if you don't need multiple identical values, you can go with TreeSet right off the bat and your life will be that much easier. <p>Liam Morley<br><A HREF="mailto:"></A><br>&quot;light the deep, and bring silence to the world.<br>light the world, and bring depth to the silence.&quot;
 
Remeber that if compareTo() returns true on this and Object o it should be true that:

this.equals(o) && o.equals(this);

Which may mean rewriting your equals, and hashcode.... A bit much... Perhaps you should define a comparator instead.

If you have to write your own sort, you have a host of different sorts to chose from...

QuickSort is most likely the fast in real time for this operation -- because it has less overhead (unless your sorting a gigantic list).

Merge Sort is mist effecient in theoretical run times, meaning it scales really well -- but it uses recurssion so it incurses a lot of overhead even when sorting smaller sets... So QuickSort is better for most small applications.

Bubble, selection and insertion are all O(n^2), which means they take a long time -- and insertion can take a lot of memory as well... All in all these are easier to write but take more time to run -- if the set is small enough no one will notice ;) .

 
I don't think QuickSort is suitable for small applications. In fact, efficient QuickSort implementations use bubble algorithm when the list size decreases.

Cheers.

Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top