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.