排序集合

来源:互联网 发布:录屏软件fast 编辑:程序博客网 时间:2024/04/19 11:46

Dictionary介绍


 private void iterateThruDictonary() {

        Dictionary<String,Element> e = BuildDictionary();
        foreach(KeyValuePair<string,Element> kvp in e){
            Element theE = kvp.Value;
            Console.WriteLine(""+kvp.Key);
            Console.WriteLine("values: " + theE.Symbol+" " + theE.Name + " "+theE.AtomicNumber);
            Console.ReadKey();
        }
    }
    static void Main(string[] args) {
        ReplaceSubstrings r = new ReplaceSubstrings();
        r.iterateThruDictonary();
    }

    private Dictionary<string, Element> BuildDictionary() {
        var elements = new Dictionary<string, Element>();

        AddToDictionary(elements, "K", "Potassium", 19);
        AddToDictionary(elements, "Ca", "Calcium", 20);
        AddToDictionary(elements, "Sc", "Scandium", 21);
        AddToDictionary(elements, "Ti", "Titanium", 22);

        return elements;
    }
    private void AddToDictionary(Dictionary<string,Element> elements,string sym,string name,int atom) {
        Element theElement = new Element();
        theElement.Symbol = sym;
        theElement.Name = name;
        theElement.AtomicNumber = atom;
        elements.Add(key:theElement.Symbol,value:theElement);

    }
    public class Element {
        public string Symbol {get;set;}
        public string Name{get;set;}
        public int AtomicNumber{get;set;}

    }



排序集合

该示例对存储在 List<T>中的Car 选件类实例进行排序。 Car 选件类实现了 IComparable<T> 接口,需执行CompareTo 方法。

每次对 CompareTo 方法的调用都会产生用于排序的单个比较。用户编写在 CompareTo 方法中的代码将为每一次当前对象和其他对象的比较返回一个值。如果当前对象小于第二个对象,则该方法返回小于零的值,如果当前对象大于第二个对象,则返回大于零的值,如果两个对象相等,则返回零。这使您可以在代码中定义标准:大于、小于或者相等。

ListCars 方法中, cars.Sort() 方法对列表进行排序。该项对 List<T>Sort 方法的调用,会导致对ListCar 对象的CompareTo 方法的自动调动。


 private void ListCars()
    {
        var cars = new List<Car>
        {
            { new Car() { Name = "car1", Color = "blue", Speed = 20}},
            { new Car() { Name = "car2", Color = "red", Speed = 50}},
            { new Car() { Name = "car3", Color = "green", Speed = 10}},
            { new Car() { Name = "car4", Color = "blue", Speed = 50}},
            { new Car() { Name = "car5", Color = "blue", Speed = 30}},
            { new Car() { Name = "car6", Color = "red", Speed = 60}},
            { new Car() { Name = "car7", Color = "green", Speed = 50}}
        };
        cars.Sort();
        foreach (Car thisCar in cars)
        {
            Console.Write(thisCar.Color.PadRight(5) + " ");
            Console.Write(thisCar.Speed.ToString() + " ");
            Console.Write(thisCar.Name);
            Console.WriteLine();
            Console.ReadKey();
        }

    }
    static void Main(string[] args) {
        ReplaceSubstrings r= new ReplaceSubstrings();
        r.ListCars();
    }
    public class Car : IComparable<Car> {
        public string Name {get;set; }
        public int Speed { get; set; }
        public string Color { get; set; }

        public int CompareTo(Car other) {//先按照颜色排序 颜色相同 按照速度排序
            int compare;
            compare = string.Compare(this.Color,other.Color,true);
            if (compare == 0) { compare = this.Speed.CompareTo(other.Speed); compare = -compare; }
            return compare;
        }
    }



0 0
原创粉丝点击