国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#中Arraylist的sort函數用法實例分析

2020-01-24 01:25:26
字體:
來源:轉載
供稿:網友

本文實例講述了C#中Arraylist的sort函數用法。分享給大家供大家參考。具體如下:

ArrayList的sort函數有幾種比較常用的重載:

1.不帶參數

2.帶一個參數

public virtual void Sort(  IComparer comparer)

參數

comparer

類型:System.Collections.IComparer

比較元素時要使用的 IComparer 實現。

- 或 -

null 引用(Visual Basic 中為 Nothing)將使用每個元數的 IComparable 實現。

示例:

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]:  dogAfter sorting with the default comparer:    [0]:  brown    [1]:  dog    [2]:  fox    [3]:  jumps    [4]:  lazy    [5]:  over    [6]:  quick    [7]:  the    [8]:  TheAfter 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 */

希望本文所述對大家的C#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 板桥市| 马龙县| 巴中市| 宁明县| 永靖县| 张家川| 临湘市| 鹤壁市| 拉孜县| 巴里| 襄垣县| 安新县| 交城县| 图片| 昌邑市| 嘉祥县| 历史| 蓬溪县| 延吉市| 乌拉特中旗| 商丘市| 那曲县| 三门县| 蓝田县| 吉木萨尔县| 湖南省| 三江| 清新县| 舟曲县| 石渠县| 任丘市| 南召县| 沈丘县| 汝阳县| 长兴县| 子长县| 宁德市| 屯留县| 晋江市| 宣武区| 阜宁县|