C#中Arraylist的sort函数用法实例分析

作者:dongfengkuayue 时间:2023-01-08 21:40:27 

本文实例讲述了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]:  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
*/

希望本文所述对大家的C#程序设计有所帮助。

标签:C#,Arraylist,sort
0
投稿

猜你喜欢

  • 基于ReentrantLock的实现原理讲解

    2023-11-23 22:43:23
  • 第一次使用Android Studio时你应该知道的一切配置(推荐)

    2022-01-08 00:49:38
  • Tablayout简单使用方法总结

    2022-01-08 16:27:37
  • Java抽象类的概念讲解

    2023-11-04 13:40:35
  • Java中的静态绑定和动态绑定详细介绍

    2023-01-18 19:54:06
  • Json读写本地文件实现代码

    2023-10-10 06:03:21
  • 分别在Groovy和Java中创建并初始化映射的不同分析

    2023-11-26 08:15:42
  • Spring源码解析之编程式事务

    2023-06-20 19:17:49
  • java8 集合之Stack详解及实例

    2023-08-02 16:04:07
  • Unity实战之FlyPin(见缝插针)小游戏的实现

    2022-05-21 19:46:31
  • 深入讲解我们说的CAS自旋锁到底是什么

    2022-05-21 14:43:08
  • WPF实现倒计时转场动画效果

    2023-01-10 11:46:23
  • java定义受限制的类型参数操作

    2022-12-16 09:44:03
  • 关于通过java调用datax,返回任务执行的方法

    2023-11-28 21:26:45
  • Android通过wifi连接手机(不需要root)

    2023-10-12 00:33:23
  • List<>中Find的用法小结

    2022-05-25 18:41:44
  • Android Beam 文件传输失败分析与解决方法

    2023-03-16 18:15:27
  • C#对集合进行排序

    2022-06-10 02:08:03
  • WinForm中Application.Idle方法详解

    2022-09-05 01:53:06
  • Android抢红包助手开发全攻略

    2023-01-10 23:03:40
  • asp之家 软件编程 m.aspxhome.com