ArrayList.Sort method in C#
2004-08-16 20:39
369 查看
Today I first used the sort method of ArrayList in C#.
In order to sort the data under my business logic I designed a class inherit the IComparer interface,
then I realized the method -- Compare of the interface,in which I coded some logic to
compare two objects and return a value indication whether one is less than,equal to
or greater than the other.
There is a simple sample here
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ArrayList myAL = new ArrayList();
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
myAL.Add("The");
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
myAL.Add("QUICK");
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
myAL.Add("BROWN");
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
IComparer myComparer = new myCompare();
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
myAL.Sort(0, 3, myComparer);
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public class myCompare : IComparer
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.cnblogs.com/Images/dot.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int IComparer.Compare(Object x, Object y)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cnblogs.com/Images/dot.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return ((new CaseInsensitiveComparer()).Compare(y, x));
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
I think the sort way of ArrayList designed in C# is great , we can sort the elements in any way we want
In order to sort the data under my business logic I designed a class inherit the IComparer interface,
then I realized the method -- Compare of the interface,in which I coded some logic to
compare two objects and return a value indication whether one is less than,equal to
or greater than the other.
There is a simple sample here
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ArrayList myAL = new ArrayList();
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
myAL.Add("The");
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
myAL.Add("QUICK");
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
myAL.Add("BROWN");
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
IComparer myComparer = new myCompare();
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
myAL.Sort(0, 3, myComparer);
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public class myCompare : IComparer
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.cnblogs.com/Images/dot.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int IComparer.Compare(Object x, Object y)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cnblogs.com/Images/dot.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return ((new CaseInsensitiveComparer()).Compare(y, x));
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
I think the sort way of ArrayList designed in C# is great , we can sort the elements in any way we want
相关文章推荐
- ArrayList.Sort method in C#
- My FactoryMethod in C#
- c#:sort在非泛型类ArrayList,泛型类List<>中的用法
- 在C#中直接使用Open API的方法 / A Method of Using Open API in C#
- (.NET) IntelliSense difference of Extension Method name in Statement Completion for VB and C#.
- C#函数编译出错:Inconsistent accessibility: parameter type ‘aaa.bbb.ccc’ is less accessible than method ‘aa
- C# 对 结构体 或 类对象 的arraylist 排序 sort()
- Part 67 to 70 Talking about method parameters in C#
- 【转】How to call c++ exported method and classes in c#
- My FactoryMethod in C#
- Mocking a method to throw an exception in C#
- How to call code behind method in JavaScript in C# and VS2010?
- C# 数组集合 <一>基本概念(Array&ArrayList&List<T>&SortList&LinkList的实现Code)
- Dynamic in C# VII: Phantom Method Semantics
- c# 学习笔记 ArrayList的Sort方法的实现
- c# ArrayList 的 Sort()方法的使用
- C#ArrayList-sort
- c# ArrayList 的 Sort()方法的使用
- Part 61 to 63 Talking about partial class and partial method in C#
- Numeric String Sort in C#