Collections.sort - do i *have* to implement List

Discussion in 'Java' started by timasmith@hotmail.com, Jun 1, 2006.

  1. Guest

    I have custom collection of my objects

    MyModelList list;

    which I wish to sort based upon the contents - which implement
    Comparable.

    Collections.sort(list);

    is a beautiful thing - BUT to implement the list interface is asking a
    bit much... there are 20 methods or more...

    Extending Vector is not an option for me since I already extend the
    AbstractTableModel so I can easily view my list in a JTable.

    What else can I do to elegantly sort my custom list?

    thanks

    Tim
    , Jun 1, 2006
    #1
    1. Advertising

  2. bugbear Guest

    wrote:
    > I have custom collection of my objects
    >
    > MyModelList list;
    >
    > which I wish to sort based upon the contents - which implement
    > Comparable.
    >
    > Collections.sort(list);
    >
    > is a beautiful thing - BUT to implement the list interface is asking a
    > bit much... there are 20 methods or more...
    >
    > Extending Vector is not an option for me since I already extend the
    > AbstractTableModel so I can easily view my list in a JTable.
    >
    > What else can I do to elegantly sort my custom list?


    If your class *contains* an (instance of) list, and you sort that,
    I think you're where you want to be.

    BugBear
    bugbear, Jun 1, 2006
    #2
    1. Advertising

  3. Chris Uppal Guest

    wrote:

    > I have custom collection of my objects
    >
    > MyModelList list;
    >
    > which I wish to sort based upon the contents - which implement
    > Comparable.
    >
    > Collections.sort(list);
    >
    > is a beautiful thing - BUT to implement the list interface is asking a
    > bit much... there are 20 methods or more...


    You could create an adapter object which implements List by forwarding to your
    MyModelList. The best way to do that is probably to start with
    java.util.AbstractList, and then override the get(int), size(), and set(int,
    Object) methods. If your MyModelList doesn't have constant-time access, then
    it may be better to start with java.util.AbstractSequentialList instead.

    If you are just wanting to sort the contents of a JTable, then google for
    java jtable sort column
    to find alternative, and maybe preferable, approaches.

    -- chris
    Chris Uppal, Jun 1, 2006
    #3
  4. wrote:
    > I have custom collection of my objects
    >
    > MyModelList list;
    >
    > which I wish to sort based upon the contents - which implement
    > Comparable.
    >
    > Collections.sort(list);
    >
    > is a beautiful thing - BUT to implement the list interface is asking a
    > bit much... there are 20 methods or more...
    >
    > Extending Vector is not an option for me since I already extend the
    > AbstractTableModel so I can easily view my list in a JTable.
    >
    > What else can I do to elegantly sort my custom list?


    You can at least:

    - Subclass AbstractList instead of subclassing AbstractTableModel, and
    implement TableModel

    - You can provide a method which handles the sorting internally,
    assuming you do have a List or an Object[] inside your class:

    void sort() {
    [Arrays|Collections].sort(theListsInternalData);
    }

    - Use Arrays.sort() if you have your list elements in some form of Object[].

    - Implement some sorting algorithm by your own.

    - If you internally have a List or Object[], implement a method (not
    recommended):

    [List|Object[]] getList() {
    return theListsInternalDataStructure;
    }

    - Implement List

    /Thomas
    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
    Thomas Weidenfeller, Jun 1, 2006
    #4
  5. Thomas Weidenfeller wrote:
    > wrote:
    >>
    >> Collections.sort(list);
    >>
    >> is a beautiful thing - BUT to implement the list interface is asking a
    >> bit much... there are 20 methods or more...
    >>
    >> Extending Vector is not an option for me since I already extend the
    >> AbstractTableModel so I can easily view my list in a JTable.
    >>
    >> What else can I do to elegantly sort my custom list?

    >
    > You can at least:
    >
    > - Subclass AbstractList instead of subclassing AbstractTableModel, and
    > implement TableModel
    >
    > - [...]


    Create an array of Integer representing current indexes. Fill the array
    with 0, 1, 2, etc. Create a Comparator<Integer>, containing a
    reference to your model, that sorts indexes based on data from your
    model. Use Arrays.<Integer>sort(Integer,Comparator<Integer>). Rearrange
    your model data from array of indexes.

    Okay, perhaps not an elegant solution. More of a hack. The point is,
    data used to sort an array/List need not be directly referenced by the
    collection. More generally keeping a key to data can be more effective
    than keeping a na\"ive reference (say, if you have a terabyte database).

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
    Thomas Hawtin, Jun 1, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doug Poland
    Replies:
    9
    Views:
    728
    VisionSet
    Sep 27, 2003
  2. asil klin
    Replies:
    28
    Views:
    1,297
    Andreas Leitgeb
    Mar 5, 2011
  3. mutex
    Replies:
    0
    Views:
    211
    mutex
    Jul 27, 2003
  4. Navin
    Replies:
    1
    Views:
    690
    Ken Schaefer
    Sep 9, 2003
  5. Luke Matuszewski
    Replies:
    1
    Views:
    148
    Thomas 'PointedEars' Lahn
    Mar 3, 2006
Loading...

Share This Page