G
Guest
Sorts great!...but in only one direction.
We would like to have the list descending as well.
We would like to have the list descending as well.
Sean said:Sorts great!...but in only one direction.
We would like to have the list descending as well.
Chris Mullins said:... 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.
Chris Mullins said:Chris Mullins said:... 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.