📄 4.5.txt
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -