---------------------------------------------------------------- Employeeid. CS (key)
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Collections;public class EmployeeID : IComparable<EmployeeID>{ public int ID { get; private set; } public EmployeeID(int id) { this.ID = id; } public int CompareTo(EmployeeID other) { return ID.CompareTo(other.ID); }}
---------------------------------------------------------------- Person. CS
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication1{ public class Person { public string Country { get; private set; } public string Name { get; private set; } public int Age { get; private set; } public Person(string country, string name, int age) { this.Country = country; this.Name = name; this.Age = age; } }}
-------------------------------------------------------------- Main program
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace consoleapplication1 {class program {static void main (string [] ARGs) {// sortedlist <tkey, tvalue> uses less memory // sorteddictionary <tkey, tvalue> fast element insertion and deletion // The key must implement the icomparable <in T> interface sorteddictionary <employeeid, person> SD = new sorteddictionary <employeeid, person> (); SD. add (New employeeid (3), new person ("China", "Zhang Fei", 40); SD. add (New employeeid (20), new person ("China", "Guan Yu", 43); SD. add (New employeeid (4), new person ("China", "Liu Bei", 45); SD. add (New employeeid (5), new person ("China", "Zhuge Liang", 24); SD. add (New employeeid (1), new person ("USA", "clerk", 40); SD. add (New employeeid (0), new person ("USA", "Obama", 40); SD. add (New employeeid (210), new person ("North Korea", "Jin San fat", 40); SD. add (New employeeid (80), new person ("India", "India a 3", 40); foreach (VAR item in SD) {console. writeline (item. key. ID + "," + item. value. name); // key, positive order} console. readkey ();}}}
This article from the "programmer's home" blog, please be sure to keep this source http://962410314.blog.51cto.com/7563109/1548070
Dictionary --- ordered Dictionary (sorteddictionary <tkey, tvalue>)