C#中Arraylist的sort函數(shù)用法實(shí)例分析
本文實(shí)例講述了C#中Arraylist的sort函數(shù)用法。分享給大家供大家參考。具體如下:
ArrayList的sort函數(shù)有幾種比較常用的重載:
1.不帶參數(shù)
2.帶一個(gè)參數(shù)
public virtual void Sort( IComparer comparer )
參數(shù)
comparer
類(lèi)型:System.Collections.IComparer
比較元素時(shí)要使用的 IComparer 實(shí)現(xiàn)。
- 或 -
null 引用(Visual Basic 中為 Nothing)將使用每個(gè)元數(shù)的 IComparable 實(shí)現(xiàn)。
示例:
using System;
using System.Collections;
public class SamplesArrayList {
public class myReverserClass : IComparer {
// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare( Object x, Object y ) {
return( (new CaseInsensitiveComparer()).Compare( y, x ) );
}
}
public static void Main() {
// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add( "The" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
myAL.Add( "jumps" );
myAL.Add( "over" );
myAL.Add( "the" );
myAL.Add( "lazy" );
myAL.Add( "dog" );
// Displays the values of the ArrayList.
Console.WriteLine( "The ArrayList initially contains the following values:" );
PrintIndexAndValues( myAL );
// Sorts the values of the ArrayList using the default comparer.
myAL.Sort();
Console.WriteLine( "After sorting with the default comparer:" );
PrintIndexAndValues( myAL );
// Sorts the values of the ArrayList using the reverse case-insensitive comparer.
IComparer myComparer = new myReverserClass();
myAL.Sort( myComparer );
Console.WriteLine( "After sorting with the reverse case-insensitive comparer:" );
PrintIndexAndValues( myAL );
}
public static void PrintIndexAndValues( IEnumerable myList ) {
int i = 0;
foreach ( Object obj in myList )
Console.WriteLine( "\t[{0}]:\t{1}", i++, obj );
Console.WriteLine();
}
}
/*
This code produces the following output.
The ArrayList initially contains the following values:
[0]: The
[1]: quick
[2]: brown
[3]: fox
[4]: jumps
[5]: over
[6]: the
[7]: lazy
[8]: dog
After sorting with the default comparer:
[0]: brown
[1]: dog
[2]: fox
[3]: jumps
[4]: lazy
[5]: over
[6]: quick
[7]: the
[8]: The
After sorting with the reverse case-insensitive comparer:
[0]: the
[1]: The
[2]: quick
[3]: over
[4]: lazy
[5]: jumps
[6]: fox
[7]: dog
[8]: brown
*/
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
- C#常見(jiàn)的幾種集合 ArrayList,Hashtable,List<T>,Dictionary<K,V> 遍歷方法對(duì)比
- C#檢查指定對(duì)象是否存在于ArrayList集合中的方法
- C#中Array與ArrayList用法及轉(zhuǎn)換的方法
- C#中ArrayList的使用方法
- 淺析C#中數(shù)組,ArrayList與List對(duì)象的區(qū)別
- C#.Net ArrayList的使用方法
- C#查找對(duì)象在ArrayList中出現(xiàn)位置的方法
- c# ArrayList的使用方法小總結(jié)
- C#中數(shù)組、ArrayList、List、Dictionary的用法與區(qū)別淺析(存取數(shù)據(jù))
- C#生成隨機(jī)ArrayList的方法
- C#中數(shù)組、ArrayList和List三者的區(qū)別詳解及實(shí)例
- C#入門(mén)教程之集合ArrayList用法詳解
相關(guān)文章
C#實(shí)現(xiàn)ComboBox變色的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)ComboBox變色的效果,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2023-01-01
winform下實(shí)現(xiàn)win7 Aero磨砂效果實(shí)現(xiàn)代碼
winform下實(shí)現(xiàn)win7 Aero磨砂效果實(shí)現(xiàn)代碼,需要的朋友可以參考下2012-03-03
WinForm實(shí)現(xiàn)程序一段時(shí)間不運(yùn)行自動(dòng)關(guān)閉的方法
這篇文章主要介紹了WinForm實(shí)現(xiàn)程序一段時(shí)間不運(yùn)行自動(dòng)關(guān)閉的方法,涉及WinForm計(jì)時(shí)器及進(jìn)程操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
基于WPF實(shí)現(xiàn)3D畫(huà)廊動(dòng)畫(huà)效果的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何基于WPF實(shí)現(xiàn)簡(jiǎn)單的3D畫(huà)廊動(dòng)畫(huà)效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02
c# 數(shù)據(jù)庫(kù)的 sql 參數(shù)封裝類(lèi)的編寫(xiě)
c# 數(shù)據(jù)庫(kù)的 sql 參數(shù)封裝類(lèi)的編寫(xiě)...2007-12-12
C#實(shí)現(xiàn)將記事本中的代碼編譯成可執(zhí)行文件的方法
這篇文章主要介紹了C#實(shí)現(xiàn)將記事本中的代碼編譯成可執(zhí)行文件的方法,很實(shí)用的技巧,需要的朋友可以參考下2014-08-08
C#如何Task執(zhí)行任務(wù),等待任務(wù)完成
這篇文章主要介紹了C#如何Task執(zhí)行任務(wù),等待任務(wù)完成,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06

