Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. Data;
Using System. drawing;
Using System. text;
Using System. Windows. forms;
Namespace Testapp
{
Public Partial Class Form1: Form
{
Public Form1 ()
{
Initializecomponent ();
}
Private Void Button#click ( Object Sender, eventargs E)
{
Usingiteration ();
Usingcomparer ();
}
/**/ /// <Summary>
///The iterator uses the demo
/// </Summary>
Void Usingiteration ()
{
System. Text. stringbuilder sbshow = New Stringbuilder ();
System. Collections. ienumerator IE = This . Combobox1.items. getenumerator ();
While (Ie. movenext ())
{
Sbshow. appendline (ie. Current. tostring ());
}
MessageBox. Show (sbshow. tostring ());
/**/ /* display
--- line1 ---
--- line5 ---
--- line3 ---
--- line2 ---
--- line4 ---
--- line7 ---
--- line8 ---
--- line6 ---
*/
}
/**/ /// <Summary>
///List sorting demo
/// </Summary>
Void Usingcomparer ()
{
Stringsort sort = New Stringsort ();
System. Collections. arraylist arrlist = New System. Collections. arraylist (combobox1.items );
Arrlist. Sort (SORT );
System. Text. stringbuilder sbshow = New Stringbuilder ();
For ( Int I = 0 ; I < Arrlist. Count; I ++ )
{
Sbshow. appendline (arrlist [I]. tostring ());
}
MessageBox. Show (sbshow. tostring ());
/**/ /*
--- line1 ---
--- line2 ---
--- line3 ---
--- line4 ---
--- line5 ---
--- line6 ---
--- line7 ---
--- line8 ---
*/
}
}
Public Class Stringsort: system. Collections. icomparer
{
Public Int Compare ( Object X, Object Y)
{
String Str1 = X As String ;
String Str2 = Y As String ;
If (Str1 = Null | Str2 = Null )
{
Throw NewException ("Item error!");
}
Else
{
If (Intgetindex (str1) < Intgetindex (str2 ))
{
Return -1;
}
Else
{
Return 0;
}
}
}
Int Intgetindex ( String Str)
{
ReturnConvert. toint32 (Str. substring (7,1));//--- Line1 ---
}
}
}