|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Generics SortedList QuestionSorts great!...but in only one direction.
We would like to have the list descending as well. "Sean" <S***@discussions.microsoft.com> wrote .... so write a different comparer, and pass that into the sort method. > Sorts great!...but in only one direction. > > We would like to have the list descending as well. > 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. -- Chris Mullins "Chris Mullins" <cmull***@yahoo.com> wrote in message The code looks like:news:ODzm3NYPGHA.3164@TK2MSFTNGP11.phx.gbl... > "Sean" <S***@discussions.microsoft.com> wrote >> 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. 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. -- Chris Mullins Thanks I will look more into that code you wrote.
Show quote "Chris Mullins" wrote: > "Chris Mullins" <cmull***@yahoo.com> wrote in message > news:ODzm3NYPGHA.3164@TK2MSFTNGP11.phx.gbl... > > "Sean" <S***@discussions.microsoft.com> wrote > >> 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. > > 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. > > -- > Chris Mullins > > > I wouldn't even bother to wate time in the multiplication (for -1). It
should be sufficient to reverse operands in the compare ;-) -tom Show quote > > 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. > > -- > Chris Mullins |
|||||||||||||||||||||||