Collection and Object Ordering

Source: Internet
Author: User
Tags foreach constructor interface sort
Object. Net SDK provides a number of collection classes in the System.Collections namespace.
We Use this collection classes to store objects inside them and perform some
On them later in 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 be to use Sort ()
To order elements stored in a collection class in a generic fashion.

Let me explain your 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. The Getcountry returns the name of the country and the second method Getcapital returns the name of the capital O F that country. Apart from those two methods there are constructor, which accepts name and capital name as country and parameters th e 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 the 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 Icompa RER 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 the inside. The the parameter is used for comparing the objects stored inside the ArrayList and second parameter helps in ordering T The He object in either ascending or descending the order.

As you are the main method, there are five country objects with different country name and their capitals and there are A ArrayList object to store those country objects. The 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 on ascending order, to does this we are passing Genericsort object as parameter to Sort method.

Step 3:now If your compile and run this program you would to following output:

Before sorting
USA
Canada
France
Australia
Mexico

After sorting in ascending order
Australia
Canada
France
Mexico
USA

So, while next time you need to order elements inside a collection object take this sample and modify it accordingly to Sui T your need. Most of the time the Genericsort object should meet your requirement any without.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.