Connecting Tech Pros Worldwide Help | Site Map

Sort a vector in J2ME

Familiar Sight
 
Join Date: Nov 2007
Posts: 153
#1: Aug 26 '09
hi,

I have searched without success on a solution to sort a vector in j2me in a desired way. There doesn't seem to be a Comparable class. My vector contains Product objects. What have people done in the past for a workaround?

Regards

Brendan
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,387
#2: Aug 27 '09

re: Sort a vector in J2ME


You have to write a comparable method in your Product class. Then you can just do vector.sort() or whatever collection you are using. It will sort itself using your comparable method. I think you class has to implement Comparator or something like that.

I think its a static so Collecitons.sort(vector); it's been a few years since I used Java.

This should be covered in the J2ME documentation.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#3: Aug 27 '09

re: Sort a vector in J2ME


Quote:

Originally Posted by brendanmcdonagh View Post

hi,

I have searched without success on a solution to sort a vector in j2me in a desired way. There doesn't seem to be a Comparable class. My vector contains Product objects. What have people done in the past for a workaround?

Comparable is an interface and your Product class has to implement it; if that interface is not present in JME is the Comparator interface present? If so you do the same but implement the actual comparison in the Comparator interface implementation instead of in the Product class. Is there a Collections utility class? If so, a sort method is available as well, otherwise there's an article in the Java insights section that shows you an efficient heap sort algorithm.

kind regards,

Jos
Reply