Sortedlist that does not sort or can duplicate keys.

Source: Internet
Author: User

My colleague wants to implement a dictionary table that retains and adds the sequence. hashtableb cannot meet the requirements. The normal sortedlist will be automatically sorted. After reading the help, the implementation method is very simple,CodeAs follows:

Using System;
Using System. collections;

Namespace Testsortedlist
{
Class Class1
{
[Stathread]
Static   Void Main ( String [] ARGs)
{
Sortedlist SL =   New Sortedlist ( New Mysort ()); // Unordered
SL. Add ( 333 , 333 );
SL. Add ( 111 , 111 );
SL. Add ( 222 , 222 );
SL. Add ( 111 , 112 );

Printlist (SL );

Console. Readline ();
}

Private   Static   Void Printlist (sortedlist SL)
{
For ( Int I = 0 ; I SL. Count; I ++ )
{
Console. writeline ( " {0} \ t {1} " , SL. getkey (I), SL. getbyindex (I ));
} // End
} // End FN ()

}
Public   Class Mysort: icomparer
{
# Region Icomparer Member
Public   Int Compare ( Object X, Object Y)
{
Return   - 1 ;

// Sort
// Int iresult = (INT) x-(INT) y;
// If (iresult = 0) iresult =-1;
// Return iresult;

}
# Endregion
}

}

As long as the return result of the Compare function is not equal to 0, the same key can be added, which can be sorted and has the same key value, which may be used in some cases.

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.