using System;
using System.Collections.Generic;
public class Example
{
private static int CompareDinosByLength(object x, object y)
{
if (x == null)
{
if (y == null)
{
return 0;
}
else
{
return -1;
}
}
else
{
if (y == null)
{
return 1;
}
else
{
Person personX = (Person)x;
Person personY = (Person)y;
int retval = personX.Name.CompareTo(personY.Name);
if (retval != 0)
{
return retval;
}
else
{
return personX.Name.CompareTo(personY.Name);
}
}
}
}
public static void Main()
{
List<Person> persons = new List<Person>();
persons.Add(new Person("1", "alily"));
persons.Add(new Person("8", "eocs"));
persons.Add(new Person("7", "cjan"));
persons.Add(new Person("3", "btommy"));
persons.Add(new Person("5", "dshel"));
persons.Sort(CompareDinosByLength);
Display(persons);
}
private static void Display(List<Person> list)
{
Console.WriteLine();
foreach (Person p in list)
{
if (p == null)
Console.WriteLine("(null)");
else
Console.WriteLine("\"{0}----{1}\"", p.ID,p.Name);
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Person
/// </summary>
class Person
{
string _id = string.Empty;
string _name = string.Empty;
public string Name
{
set { this._name = value; }
get { return this._name; }
}
public string ID
{
set { this._id = value; }
get { return this._id; }
}
public Person(string id, string name)
{
this.ID = id;
this.Name = name;
}
}
浙公网安备 33010602011771号