Sort a vector in J2ME

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brendanmcdonagh
    New Member
    • Nov 2007
    • 153

    Sort a vector in J2ME

    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
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    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.sor t(vector); it's been a few years since I used Java.

    This should be covered in the J2ME documentation.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by brendanmcdonagh
      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

      Comment

      Working...