Collection and Object Ordering
2024-07-21 02:22:06
供稿:网友
.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.