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 / March 2006

Tip: Looking for answers? Try searching our database.

Generics SortedList Question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sean - 01 Mar 2006 22:52 GMT
Sorts great!...but in only one direction.

We would like to have the list descending as well.
Chris Mullins - 01 Mar 2006 22:57 GMT
> Sorts great!...but in only one direction.
>
> We would like to have the list descending as well.

... so write a different comparer, and pass that into the sort method.
Comparers are generally trivial to write (less than 10 lines of code,
usually more like 3 or 4), and overloaded sort functions can take them as
arguments.

Signature

Chris Mullins

Chris Mullins - 01 Mar 2006 23:08 GMT
>> Sorts great!...but in only one direction.
>>
[quoted text clipped - 4 lines]
> usually more like 3 or 4), and overloaded sort functions can take them as
> arguments.

The code looks like:

private void DoSomething()
{
List<string> foo = new List<string>();
foo.Add("a");
foo.Add("b");
foo.Add("z");
foo.Add("e");
foo.Add("j");
foo.Sort(new StringReverseComparer());
MessageBox.Show(foo[0]);
}

public class StringReverseComparer : IComparer<string>
{
   public virtual int Compare(string x, string y)
   {
       return x.CompareTo(y) * -1;
   }
}

The framework already has all the relevant comparers for the basic types, so
this isn't something you would normally do for a string collection, but the
point is valid for any generic type.

Signature

Chris Mullins

Sean - 02 Mar 2006 02:09 GMT
Thanks I will look more into that code you wrote.

> >> Sorts great!...but in only one direction.
> >>
[quoted text clipped - 30 lines]
> this isn't something you would normally do for a string collection, but the
> point is valid for any generic type.
tommaso.gastaldi@uniroma1.it - 02 Mar 2006 14:07 GMT
I wouldn't even bother to wate time in the multiplication (for -1). It
should be sufficient to reverse operands in the compare ;-)

-tom

> public class StringReverseComparer : IComparer<string>
> {
[quoted text clipped - 7 lines]
> this isn't something you would normally do for a string collection, but the
> point is valid for any generic type.

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.