C#中Arraylist的sort函数用法实例分析
作者:dongfengkuayue 发布时间:2023-01-08 21:40:27
标签:C#,Arraylist,sort
本文实例讲述了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#程序设计有所帮助。


猜你喜欢
- 本文实例讲述了Java编程调用微信接口实现图文信息等推送功能。分享给大家供大家参考,具体如下:Java调用微信接口工具类,包含素材上传、获取
- 这篇文章主要介绍了java多线程加锁以及Condition类的使用实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参
- 今天收到了Android Studio3.0更新推送,在升级过程中遇到几个问题,在这里把问题和解决方法记录下,方便要升级的童鞋。如果还有童
- 在Java的内存分配中,总共3种常量池:Java 常量池详解(二)class文件常量池 和 Java 常量池详解(三)class运行时常量池
- 本文实例讲述了使用adb命令向Android模拟器中导入通讯录联系人的方法。分享给大家供大家参考。具体实现方法如下:使用adb提供的命令,
- 关于MouseWheelListener的鼠标滚轮事件Java中JPanel面板中对鼠标滚轮事件的处理。一、MouseWheelListen
- java的比较器有两类,分别是Comparable接口和Comparator接口。在为对象数组进行排序时,比较器的作用非常明显,首先来讲解C
- 一、在相应的板块中开启DataBinding dataBinding {
- 这篇文章主要介绍了JAVA内存溢出解决方案图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参
- 前言上一篇我们介绍了 Animation 和 AnimationController 的使用,这是最
- 解决My eclipse 工程发布时端口占用问题如果运行后如图的错,需要进行如下操作来解决:a:打开cmd,输入netstat -ano 找
- 本文实例形式展示了C#中异步调用的实现方法,并对其原理进行了较为深入的分析,现以教程的方式分享给大家供大家参考之用。具体如下:首先我们来看一
- 一、Java类的加载顺序引用1个网上的经典例子,并做稍许改动,以便大家更好地理解。public class Animal {
- RocketMq消息处理RocketMq消息处理整个流程如下:本系列RocketMQ4.8注释github地址,希望对大家有所帮助,要是觉得
- Intellij Idea打包Java项目打开模块设置如下图所示,选中需要打包的模块,右键点击,即会出现如下图所示的内容,点击Open Mo
- 关于为什么需要创建单例?这里不过多介绍,具体百度知。 关于C# 创建单例步骤或条件吧1、声明静态变量;2、私有构造函
- 动态获取对象的性能值,这个在开发过程中经常会遇到,这里我们探讨一下何如高性能的获取属性值。为了对比测试,我们定义一个类Peoplepubli
- 其实是可以通过@Constraint来限定自定义注解的方法。@Constraint(validatedBy = xxxx.class)下面是
- C#与C++ dll之间传递字符串string wchar_t* char* IntPtr1、由C#向C++ dll 传入字符串时,参数直接
- dom4j是一个非常优秀的Java XML API,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源工具。可以在这个地址ht