Rovan

      一个犁牛半块田,收也凭天,荒也凭天, 清茶淡饭饱三餐,早也香甜,晚也香甜, 布衣得暖胜丝绵,长也可穿,短也可穿, 草舍茅屋有几间,行也安然,待也安然, 雨过天青驾小船,鱼在一边,酒在一边, 夜归儿女话灯前,今也有言,古也有言, 日上三竿我独眠,请是神仙,我是神仙.

首页 新随笔 联系 订阅 管理

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;

    }
}

 

 

posted on 2007-07-23 12:27  Ruxuan  阅读(257)  评论(1)    收藏  举报