These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Sort and BinarySearch in IList<T>
PostPosted: Fri Mar 14, 2008 2:50 pm 
Newbie

Joined: Fri Mar 14, 2008 2:32 pm
Posts: 3
Hi everybody

I'm switching my application to NHibernate

I'm trying to change all List<T> to IList<T>, but i lost the sort method : sort(IComparer<T>), and the method BinarySearch(T, IComparer<T>)

Otherwise, i sow another methods that can suply this lost. But i dont know what way to choose. Can anyone help me?

Or maybe, could I use a sort method for the IList<T> using the class IComparer<T> in NHibernate?

Any Ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 14, 2008 3:44 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
We're using a wrapper for hibernates list implementation which implements the usual interfaces like IList, ICollection, ... most of the time I'm passing the calls through to the inner list. Additionally the wrapper supplies some functions like sorting. Other possibility is implementing your own ILists for hibernate (there're some interfaces from hibernate to implement), but I think that's more work to do.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 14, 2008 4:03 pm 
Newbie

Joined: Fri Mar 14, 2008 2:32 pm
Posts: 3
Could you show my a exemple using sorting ?

Thanks for the help


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 14, 2008 4:51 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Code:
public abstract class ListBase<T> :
               IList<T>,
               IEnumerable<T>,
               IRaiseItemChangedEvents
               where T : class, INotifyPropertyChanged
    {

        /// <summary>
        /// Hibernates IList
        /// </summary>
        protected IList<T> items;       
...
        public virtual void Sort( Comparison<T> comparer )
        {
            // Iteration durch die Liste
            for ( int i = 0; i < items.Count - 1; i++ )
            {
                for ( int j = i + 1; j < items.Count; j++ )
                {
                    if ( comparer( items[i], items[j] ) > 0 )
                    {
                        T temp = items[i];
                        items[i] = items[j];
                        items[j] = temp;
                    }
                }
            }

            // raise Reset Event
            RaiseListChangedEvent( new ListChangedEventArgs( ListChangedType.Reset, -1 ) );
        }
...
}


and the mapping for this list in a class

Code:
    <component name="PartnerItems" class="PartnerList">
      <bag name="Items" table="sx.bus_partner" lazy="false" order-by="orderby asc" cascade="all-delete-orphan">
        <key column="containeruid"/>
        <one-to-many class="Partner"/>
      </bag>
    </component>


where Partnerlist is derived from ListBase<Partner>.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2008 9:49 pm 
Newbie

Joined: Thu Dec 14, 2006 7:08 pm
Posts: 11
Alternatively, investigate ArrayList.Adapter(). It wraps the underlying storage of IList and provides sorting and searching.

For example,

IList<Example> ExampleList = new List<Example>();

ArrayList.Adapter(ExampleList as IList).Sort(ExampleComparer);

I hope that helps.

Cheers,
Aeden


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2008 9:50 pm 
Newbie

Joined: Thu Dec 14, 2006 7:08 pm
Posts: 11
Alternatively, investigate ArrayList.Adapter(). It wraps the underlying storage of IList and provides sorting and searching.

For example,

IList<Example> ExampleList = new List<Example>();

ArrayList.Adapter(ExampleList as IList).Sort(ExampleComparer);

From my understanding .NET's quicksort algorithm gets used. I hope that helps.

Cheers,
Aeden


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.