Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / June 2007

Tip: Looking for answers? Try searching our database.

I want an "autosort" class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jeff Johnson - 07 Jun 2007 21:22 GMT
Unless a project particularly interests me, I'll try to see if someone else
has created what I need rather than doing it myself. Such is the case now.
I'm wondering if anyone has made a list/array class (preferrably a generic
one) that holds IComparable[<T>] objects and automatically sorts the list
when a new object is added. I know there is the SortedList<TKey, TValue>
class (and SortedDictionary), but I don't want a key/value pair; I simply
want this list to contain a value and always return those values in sorted
order. For example, I want to do this:

   private SortedArray<string> mySortedArray = new SortedArray<string>();

   mySortedArray.Add("Orange");
   mySortedArray.Add("Peach");
   mySortedArray.Add("Banana");
   mySortedArray.Add("Apple");

   foreach (string fruit in mySortedArray)
   {
       Debug.WriteLine(fruit);
   }

and have the output be

Apple
Banana
Orange
Peach

without ever having to call mySortedArray.Sort() or anything like that. Has
someone done this? Am I totally blind and can't find it in the base class
library even though it's right under my nose?
Jon Skeet [C# MVP] - 07 Jun 2007 22:37 GMT
> Unless a project particularly interests me, I'll try to see if someone else
> has created what I need rather than doing it myself. Such is the case now.
[quoted text clipped - 4 lines]
> want this list to contain a value and always return those values in sorted
> order.

Yes, I've done it. In fact, it was doing slightly more than that - it
was keeping the length at a certain maximum too. I was picking the best
n possibilities out of *lots*, and didn't want them to all have to be
in memory.

I just started from scratch - didn't even implement all of IList<T> as
I didn't really need to. Just had an indexer, enumerator, Count, Add,
and a Last property which returned null or the last entry, depending on
whether or not it had hit the maximum value.

All I did was use BinarySearch on the (private) list in the call to
Add, in order to find the right insert point.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.