ASP. NET startkit timetracker (custom collection class sorting () method)

Source: Internet
Author: User

Many custom collection classes are defined in ASP. NET startkit timetracker.
For example:
Userscollection
Timeentriescollection
And so on.

They are all subclasses of the arraylist class.

For example:
Timeentriescollection stores custom timeentry classes

These custom collection classes all implement the sorting method sort

First read the section I wroteCode:

Using System;
Using System. collections;

Namespace Arraylistsort
{
Class Class1
{
[Stathread]
Static   Void Main ( String [] ARGs)
{
Arraylist = New Arraylist ( 8 );
A. Add ( 111 );
A. Add ( 2 );

A. Sort ();

Ienumerator = A. getenumerator ();
While (Ienumerator. movenext ())
{
Console. writeline (ienumerator. Current. tostring ());
}

Arraylist B = New Arraylist ( 8 );
B. Add ( New Class2 ( " Wo " ));
B. Add ( New Class2 ( " AI " ));
B. Add ( New Class2 ( " I " ));

B. Sort ();

Ienumerator iienumerator = B. getenumerator ();
While (Iienumerator. movenext ())
{
Console. writeline (class2) iienumerator. Current). );
}

Console. Readline ();
}
}

Public   Class Class2
{

Public Class2 ( String Str)
{
This.=STR;
}

String A;

Public   String A
{< br> Get
{< br> return A;
}
}

}


}

The running result is

The sorting result of A is displayed.
An error occurred while sorting B.

Why is such a result?
Arraylist's sort method uses the icomparable API for sorting.
This means that each object in the arraylist needs to implement the icomparable interface.
Int, string, and other system-defined types have implemented the icomparable interface.
A. Sort () method runs correctly
B. Sort (); an error occurs because the icomparable interface is not implemented.

Modify the class2 code,ProgramYou can run it correctly.

Public   Class Class2: icomparable
{

Public Class2 ( String Str)
{
This.=STR;
}

String A;

Public   String A
{< br> Get
{< br> return A;
}
}


Public   Int Compareto ( Object O)
{
Return This. A. compareto (class2) O). );
}

}

In fact, there is another way to achieve sorting
Use sort (icomparer)
Define the class implementation interface system. Collections. icomparer
The Code is as follows:

Public   Class Class2_sort: system. Collections. icomparer
{
// Ascending or not
Bool Isasc;

// The default sorting method is ascending.
Public Class2_sort ()
{
Isasc=True;
}

Public Class2_sort ( Bool B)
{
Isasc=B;
}

Public Int Compare ( Object X, Object Y)
{
If (Isasc)
{
//Ascending
Return(Class2) x). A. compareto (class2) Y). );
}
Else
{
//Descending Order
Return(Class2) Y). A. compareto (class2) x). );
}
}
}

B. Sort (New class2_sort (false ));
You can sort data in descending order.

I personally think this method is highly flexible.
Recommended

Summary:
1. to sort the arrays of your own objects, implement the icomparable interface on this object.
A compareto method must be defined in the object to specify the method for comparing the object.
2. to define a specific method for comparing objects, create a class that implements the icomparer interface.
This class must contain a compare method that accepts the two objects to be compared.
It helps you understand how you want to compare objects and what objects are allowed to use the defined interfaces.

 

I hope the Knowledge mentioned above will prompt you.
Of course, you are welcome to contact us and correct us.

Author: aierong
Blog: http://www.cnblogs.com/aierong
Email: aierong@126.com

Related Article

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.