using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace kq.Research.Basics
{
    class Key : IComparable<Key>
    {
        public Key()
        {
        }

        public Key(string name, int number)
        {
            Name = name;
            Number = number;
        }

        public string Name { get; set; }
        public int Number { get; set; }

        #region IComparable<Simple> Members
        public int CompareTo(Key other)
        {
            if (other == null)
                return 1;

            int result = Name.CompareTo(other.Name);
            
            if (result == 0)
                result = Number == other.Number ? 0 : Number < other.Number ? -1 : 1;

            return result;
        }
        #endregion

        public override string ToString()
        {
            return string.Format("{0}({1})", Name, Number);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            SortedList<Key, int> items = new SortedList<Key, int>();

            items.Add(new Key("a", 1), 0);
            items.Add(new Key("b", 0), 1);
            items.Add(new Key("a", 0), 2);

            foreach (Key key in items.Keys)
                Console.WriteLine("{0} => {1}", key.ToString(), items[key]);
        }
    }
}

Rezultat:

a(0) => 2
a(1) => 0
b(0) => 1

Kategorie: C#

0 Komentarzy

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *