Results 1 to 4 of 4

Thread: array sort, comparable?

  1. #1

    Thread Starter
    Addicted Member NOMADMAN's Avatar
    Join Date
    Aug 2002
    Location
    Closer than you think
    Posts
    237

    array sort, comparable?

    I'm interested in using the sort method of the array class.
    sort(Object[] a, Comparator c)

    I have anarray of ADTs cal them X[]. This type has many public varibles in the class. I'd like to sort the array by these varibles, this possible with X.sort(X, ???);?

    Any other ideas? Or am i going to have to write the sorting method???

    Thanks!

    NOMAD

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Two options:
    1) Let X implement Comparable, implement compare and use the sort function without a Comparator argument.

    2) Don't implement Comparable, instead write a custom class that implements Comparator (could be an anonymous class) and pass it as Comparator argument.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Addicted Member NOMADMAN's Avatar
    Join Date
    Aug 2002
    Location
    Closer than you think
    Posts
    237

    Good ideas

    Thanks Corned Bee, I like idea #2.

    NOMAD

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Depending on your data type #1 might make more sense. For example I explained a guy in the C++ forum today how to sort highscores using a std::set (similar to java.util.TreeSet). A highscore type should be Comparable.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width