日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

java快速排序泛型,如何进行对C# .NET通用泛型进行快速排序?

發布時間:2025/3/19 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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通用泛型进行快速排序?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。