java快速排序泛型,如何进行对C# .NET通用泛型进行快速排序?
如何進行對C# .NET通用泛型進行快速排序?
我們經常使用List泛型進行數據的封裝,但是有時候,在某種需求下,你可能需要對這個泛型進行排序,而排序規則是根據model中的某一個屬性進行排序,這時棘手的事情來了,怎么辦?!!
這個時候我們需要自己擴展一個排序方法,以下我給出一個繼承自IComparer接口的類,此類內置好升序和降序的排序規則:
[csharp]
///
/// T為泛用類型
public class Reverser : IComparer
{
private Type type = null;
private ReverserInfo info;
///
///
///
///
public Reverser(Type type, string name, ReverserInfo.Direction direction)
{
this.type = type;
this.info.name = name;
if (direction != ReverserInfo.Direction.ASC)
this.info.direction = direction;
}
///
///
///
///
public Reverser(string className, string name, ReverserInfo.Direction direction)
{
try
{
this.type = Type.GetType(className, true);
this.info.name = name;
this.info.direction = direction;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
///
///
///
///
public Reverser(T t, string name, ReverserInfo.Direction direction)
{
this.type = t.GetType();
this.info.name = name;
this.info.direction = direction;
}
//必須!實現IComparer的比較方法。
int IComparer.Compare(T t1, T t2)
{
object x = this.type.InvokeMember(this.info.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t1, null);
object y = this.type.InvokeMember(this.info.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t2, null);
if (this.info.direction != ReverserInfo.Direction.ASC)
Swap(ref x, ref y);
return (new CaseInsensitiveComparer())。Compare(x, y);
}
//交換操作數
private void Swap(ref object x, ref object y)
{
object temp = null;
temp = x;
x = y;
y = temp;
}
}
///
public struct ReverserInfo
{
///
public enum Direction
{
ASC = 0,
DESC,
};
public enum Target
{
CUSTOMER = 0,
FORM,
FIELD,
SERVER,
};
public string name;
public Direction direction;
public Target target;
}
///
/// T為泛用類型
public class Reverser : IComparer
{
private Type type = null;
private ReverserInfo info;
///
///
///
///
public Reverser(Type type, string name, ReverserInfo.Direction direction)
{
this.type = type;
this.info.name = name;
if (direction != ReverserInfo.Direction.ASC)
this.info.direction = direction;
}
///
///
///
///
public Reverser(string className, string name, ReverserInfo.Direction direction)
{
try
{
this.type = Type.GetType(className, true);
this.info.name = name;
this.info.direction = direction;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
///
///
///
///
public Reverser(T t, string name, ReverserInfo.Direction direction)
{
this.type = t.GetType();
this.info.name = name;
this.info.direction = direction;
}
//必須!實現IComparer的比較方法。
int IComparer.Compare(T t1, T t2)
{
object x = this.type.InvokeMember(this.info.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t1, null);
object y = this.type.InvokeMember(this.info.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t2, null);
if (this.info.direction != ReverserInfo.Direction.ASC)
Swap(ref x, ref y);
return (new CaseInsensitiveComparer())。Compare(x, y);
}
//交換操作數
private void Swap(ref object x, ref object y)
{
object temp = null;
temp = x;
x = y;
y = temp;
}
}
///
public struct ReverserInfo
{
///
public enum Direction
{
ASC = 0,
DESC,
};
public enum Target
{
CUSTOMER = 0,
FORM,
FIELD,
SERVER,
};
public string name;
public Direction direction;
public Target target;
}
此時寫好后,就只需要知道如何調用就行了:
[csharp]
Reverser reverser = new Reverser(typeof(Model), “btnIndex”, ReverserInfo.Direction.ASC);
Reverser reverser = new Reverser(typeof(Model), “btnIndex”, ReverserInfo.Direction.ASC);我們通過這樣的調用方式,就得到一個定義好的排序規則 reverser,假設我們的泛型實例名為 list,則按照如下調用:
[csharp]
list.Sort(reverser);
list.Sort(reverser);
至此,就完成了特定的排序規則處理了。
后感:
雖然這種需求不多,但是在自定義構造的泛型中,需要根據某一屬性進行有序顯示的時候,就非常實用。
同時任何自定義的類組成的泛型集合都可以實現自定義排序,可以做成一個通用類,沒有必要每一個自定義類都去實現排序的接口。
PS:轉載來源:暗客安全網:http://www.akhack.com原帖地址:http://www.akhack.com/thread-4572-1-5.html
09-21 05:23
總結
以上是生活随笔為你收集整理的java快速排序泛型,如何进行对C# .NET通用泛型进行快速排序?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数字图像恢复matlab结论,matla
- 下一篇: C#学习小结(DAY1)