|
| |
名片设计 CorelDRAW Illustrator AuotoCAD Painter 其他软件 Photoshop Fireworks Flash |
|
.Net SDK provides a number of collection classes in the System.Collections namespace. We use these collection classes to store objects inside them and perform some operation on them later on as per application logic. Many times we need to re-order objects stored inside these collection. Most collection classes provide a method called Sort() to re-order their elements. The objective of this article is to use Sort() method to order elements stored in a collection class in a generic fashion. Let me explain you with an example : Suppose we have a ArrayList object and which contains a number of country objects inside it and now we want to order those objects in ascending/descending order based on country name. Follow these steps to create a generic sort object to accomplish the sorting in generic fashion: Step 1: Create a file Country.cs as
Country object has got two members and two methods. First method GetCountry returns the name of the country and the second method GetCapital returns the name of the capital of that country. Apart from those two methods there is a constructor, which accepts country name and capital name as parameters and sets the countryName and capitalName members with that. Step 2: Create a file GenericSort.cs as
The above object implements IComparer interface. We would be passing this object as a parameter to Sort() method of ArrayList object (which expects a object of type IComparer interface). The constructor of this object takes two parameter namely sortMethodName of type String and sortOrder of type String. These two parameter are used in reordering objects stored inside the ArrayList. The first parameter is used for comparing the objects stored inside the ArrayList and second parameter helps in ordering the object in either ascending or descending order. As you see in the main method, there are five country objects with different country name and their capitals and there is a ArrayList object to store those country objects. The first foreach loop just prints the country name in the order the objects were added to the ArrayList. Just before the second foreach loop, we are re-ordering the objects as per country name and in ascending order, to do this we are passing GenericSort object as parameter to Sort method. Step 3: Now if you compile and run this program you would see following output : Before Sorting USA Canada France Australia Mexico After Sorting in ascending order Australia Canada France Mexico USA So, when next time you need to order elements inside a collection object take this sample and modify it accordingly to suit your need. Most of the time the GenericSort object should meet your requirement without any modification. 返回类别: 教程 上一教程: 用Asp.net实现基于XML的留言簿之三 下一教程: C#里操作时间的例子! 您可以阅读与"Collection and Object Ordering"相关的教程: · Using Delegates and Events 1 (来自一本49美元/817页2002年的书《C#.net web developer\'s guide》) · Borland .NET FAQ · COM, COM+ and .NET 的区别(转) · Using Delegates and Events 2 (来自一本49美元/817页2002年的书《C#.net web developer\'s guide》) · 漫谈.Net中的自动垃圾收集(Garbage Collection)机制(转) |
| 快精灵印艺坊 版权所有 |
首页 |
||