Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » NatTable » Default sorting when no explicit sort is applied
Default sorting when no explicit sort is applied [message #1723681] Wed, 17 February 2016 08:51 Go to next message
Quang Tran is currently offline Quang TranFriend
Messages: 25
Registered: June 2012
Junior Member
I created a SortedList using a specified Comparator to determine sort order.
When NatTable is instantiated it sorts correctly but when sorted ascending, descending, and unsorted my list is unsorted again.
I want to have a default sort when there is no explicit sort is applied (unsorted)
How do I achieve that?
Re: Default sorting when no explicit sort is applied [message #1723693 is a reply to message #1723681] Wed, 17 February 2016 09:33 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Pre sort your collection before adding it to NatTable
Re: Default sorting when no explicit sort is applied [message #1723695 is a reply to message #1723681] Wed, 17 February 2016 09:38 Go to previous messageGo to next message
Quang Tran is currently offline Quang TranFriend
Messages: 25
Registered: June 2012
Junior Member
Hi Dirk,
that might be a solution for fixed data but we have a dynamic collection.
Re: Default sorting when no explicit sort is applied [message #1723707 is a reply to message #1723695] Wed, 17 February 2016 10:32 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Then you could have a look at SortedList constructor. Although I'm not sure if the NatTable sort mechanism is working with specifying a comparator that way
Re: Default sorting when no explicit sort is applied [message #1723795 is a reply to message #1723681] Thu, 18 February 2016 06:52 Go to previous message
Quang Tran is currently offline Quang TranFriend
Messages: 25
Registered: June 2012
Junior Member
NatTable sets the comparator to null when unsorting the table.
I managed to achieve my default sorting by extending NatTableComparatorChooser.
Although I'm not so happy with my solution it works for now, there might be some performance problems because rebuildComparator is called about 3 times when clicking on a column to sort

private class MyNatTableComparatorChooser<T> extends NatTableComparatorChooser<T> {

        private final Comparator<? super T> defaultComparator;

        public AlarmNatTableComparatorChooser(final SortedList<T> sortedList,
                final TableFormat<T> tableFormat) {
            super( sortedList, tableFormat );
            defaultComparator = sortedList.getComparator();
        }

        @Override
        protected void rebuildComparator() {
            super.rebuildComparator();

            // select the new default comparator
            sortedList.getReadWriteLock().writeLock().lock();
            try {
                if( sortedListComparator == null ) {
                    sortedListComparator = defaultComparator;
                    sortedList.setComparator( defaultComparator );
                }
            } finally {
                sortedList.getReadWriteLock().writeLock().unlock();
            }

        }
    }


Previous Topic:Having issue with Mapping Child-Parent in Tree
Next Topic:Inconsistent API
Goto Forum:
  


Current Time: Fri Apr 19 21:24:28 GMT 2024

Powered by FUDForum. Page generated in 0.03589 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top