我需要一个如何使用IComparable
接口的基本示例,以便我可以按升序或降序排序,并按我正在排序的对象类型的不同字段排序.
好吧,因为你使用List
它会比使用a简单得多Comparison
,例如:
Listdata = ... // sort by name descending data.Sort((x,y) => -x.Name.CompareTo(y.Name));
当然,使用LINQ你可以使用:
var ordered = data.OrderByDescending(x=>x.Name);
但是你可以List
很容易地重新引入它(用于就地重新排序); 下面是允许例如Sort
在List
与lambda语法:
using System; using System.Collections.Generic; class Foo { // formatted for vertical space public string Bar{get;set;} } static class Program { static void Main() { Listdata = new List { new Foo {Bar = "abc"}, new Foo {Bar = "jkl"}, new Foo {Bar = "def"}, new Foo {Bar = "ghi"} }; data.SortDescending(x => x.Bar); foreach (var row in data) { Console.WriteLine(row.Bar); } } static void Sort (this List source, Func selector) { var comparer = Comparer .Default; source.Sort((x,y)=>comparer.Compare(selector(x),selector(y))); } static void SortDescending (this List source, Func selector) { var comparer = Comparer .Default; source.Sort((x,y)=>comparer.Compare(selector(y),selector(x))); } }
这是一个简单的例子:
public class SortableItem : IComparable{ public int someNumber; #region IComparable Members public int CompareTo(SortableItem other) { int ret = -1; if (someNumber < other.someNumber) ret = -1; else if (someNumber > other.someNumber) ret = 1; else if (someNumber == other.someNumber) ret = 0; return ret; } #endregion }
"那很好,但是如果我想能够控制排序顺序,或者按其他字段排序呢?"
简单.我们需要做的就是为对象添加更多字段.首先,我们将为不同的排序类型添加一个字符串,然后我们将添加一个布尔值来表示我们是按降序还是按升序排序,然后添加一个字段来确定我们要搜索哪个字段.
public class SortableItem : IComparable{ public enum SortFieldType { SortNumber, SortString } public int someNumber = -1; public string someString = ""; public bool descending = true; public SortFieldType sortField = SortableItem.SortFieldType.SortNumber; #region IComparable Members public int CompareTo(SortableItem other) { int ret = -1; if(sortField == SortableItem.SortFieldType.SortString) { // A lot of other objects implement IComparable as well. // Take advantage of this. ret = someString.CompareTo(other.someString); } else { if (someNumber < other.someNumber) ret = -1; else if (someNumber > other.someNumber) ret = 1; else if (someNumber == other.someNumber) ret = 0; } // A quick way to switch sort order: // -1 becomes 1, 1 becomes -1, 0 stays the same. if(!descending) ret = ret * -1; return ret; } #endregion public override string ToString() { if(sortField == SortableItem.SortFieldType.SortString) return someString; else return someNumber.ToString(); } }
"告诉我怎么样!"
好吧,因为你问得这么好.
static class Program { static void Main() { Listitems = new List (); SortableItem temp = new SortableItem(); temp.someString = "Hello"; temp.someNumber = 1; items.Add(temp); temp = new SortableItem(); temp.someString = "World"; temp.someNumber = 2; items.Add(temp); SortByString(items); Output(items); SortAscending(items); Output(items); SortByNumber(items); Output(items); SortDescending(items); Output(items); Console.ReadKey(); } public static void SortDescending(List items) { foreach (SortableItem item in items) item.descending = true; } public static void SortAscending(List items) { foreach (SortableItem item in items) item.descending = false; } public static void SortByNumber(List items) { foreach (SortableItem item in items) item.sortField = SortableItem.SortFieldType.SortNumber; } public static void SortByString(List items) { foreach (SortableItem item in items) item.sortField = SortableItem.SortFieldType.SortString; } public static void Output(List items) { items.Sort(); for (int i = 0; i < items.Count; i++) Console.WriteLine("Item " + i + ": " + items[i].ToString()); } }