4.5.txt
来自「《Microsoft Visual C# .NET 2003开发技巧大全》源代码」· 文本 代码 · 共 41 行
TXT
41 行
Listing 4.5 Sorting Names by Last Name and First Name Using the Compare Method
in the Person Object
using System;
using System.Collections;
namespace _11_Sorting
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string input;
ArrayList alPeople = new ArrayList();
Console.Write( “Enter first and last name or ‘q’ to quit: “ );
input = Console.ReadLine();
// get list of names from user
while( input.Length != 1 || Char.ToUpper(input[0]) != ‘Q’ )
{
string[] name = input.Split( new char[]{‘ ‘}, 2 );
alPeople.Add( new Person( name[0], name[1] ));
Console.Write( “Enter first and last name or ‘q’ to quit: “ );
input = Console.ReadLine();
}
// output unsorted names
Console.WriteLine( “\nArray before sort” );
foreach( Person p in alPeople )
{
Console.WriteLine( “ {0} {1}”, p.firstName, p.lastName );
}
// sort arraylist using custom IComparer
alPeople.Sort( new Person() );
// output sorted array
Console.WriteLine( “\nArray after sort” );
foreach( Person p in alPeople )
{
Console.WriteLine( “ {0} {1}”, p.firstName, p.lastName );
}
}
}
// person definition here...
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?