我的一亩三分地 我就喜欢!
13fen  设为主页
 收藏本站
 
当前位置: > 一亩三分地:首页 > 网络编程 > net专区 > Collection and Object Ordering
热门文章排行
热门文章排行 用VB.NET设计各种形状的窗体界面一(11-23)
NETFramework1.0精简版(12-22)
.Net中的反射使用入门(12-22)
C#多线程应用技术面对面探讨(11-23)
用C#.net轻松制作不规则窗体(12-15)
精采文章排行
精采文章排行 ASP.NET与MySQL数据库简明图示入门教(11-16)
ASP.NET与MySQL数据库简明图示入门教(11-16)
ASP.NET 链接数据库基础(11-16)
webconfig的设置节点说明(11-16)
部署ASP.NET的三大技术(上)(11-16)
技术专题推荐
网管论坛交流
 

Collection and Object Ordering 

作者:   来源:   点击:   日期:2007-11-20

.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

using System;

namespace GenericSort
{
public class Country
{
private string countryName ; //Country Name
private string capitalName ; //Capital Name

public Country(string countryName, string capitalName)
{
this.countryName = countryName;
this.capitalName = capitalName;
}

public string GetCountry()
{
return countryName;
}

public string GetCapital()
{
return capitalName;
}

}
}

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


using System;
using System.Collections;
using System.Reflection;

namespace GenericSort
{
public class GenericSort : IComparer
{
String sortMethodName;
String sortOrder;

public GenericSort(String sortMethodName, String sortOrder)
{
this.sortMethodName = sortMethodName;
this.sortOrder = sortOrder;
}

public int Compare(object x, object y)
{

IComparable ic1 = (IComparable)x.GetType().GetMethod(sortMethodName).Invoke(x,null);
IComparable ic2 = (IComparable)y.GetType().GetMethod(sortMethodName).Invoke(y,null);

if( sortOrder != null && sortOrder.ToUpper().Equals("ASC") )
return ic1.CompareTo(ic2);
else
return ic2.CompareTo(ic1);
}

[STAThread]
static void Main(string[] args)
{
Country country1 = new Country("USA", "Washington D.C.");
Country country2 = new Country("Canada", "Ottawa");
Country country3 = new Country("France", "Paris");
Country country4 = new Country("Australia", "Canberra");
Country country5 = new Country("Mexico", "Mexico City");

ArrayList al = new ArrayList();
al.Add(country1);
al.Add(country2);
al.Add(country3);
al.Add(country4);
al.Add(country5);

Console.WriteLine("Before Sorting");

foreach(Country cnt in al)
{
Console.WriteLine( cnt.GetCountry());
}

Console.WriteLine("\nAfter Sorting in ascending order");
al.Sort(new GenericSort("GetCountry", "ASC"));
foreach(Country cnt in al)
{
Console.WriteLine( cnt.GetCountry());
}

}
}
}

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.  


文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【论坛讨论

   相关文章:

   文章评论:(条)
  
 请留名: 匿名评论   点击查看所有评论 网管论坛
 

  责任编辑:一分  声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。