SortedList类中的数据如何按我自定义地排序

初始化 SortedList

public SortedList(
    int capacity,
    IComparer comparer
)

参数讲解:

capacity类型:System.Int32
SortedList

 SortedList sList = new SortedList(new MyComparer());

            UserModel UserModel1 = new UserModel();
            UserModel1.Name = "test1";
            UserModel1.Age = 20;

            UserModel UserModel2 = new UserModel();
            UserModel2.Name = "test3";
            UserModel2.Age = 1;

            UserModel UserModel3 = new UserModel();
            UserModel3.Name = "test2";
            UserModel3.Age = 2;

            sList.Add("21", UserModel1);
            sList.Add("51", UserModel2);
            sList.Add("11", UserModel3);

            _cache.Set("User1", sList);

            foreach (DictionaryEntry element in sList)
            {
                string name = (string)element.Key;
                int age = (int)element.Value;
            }

你可能感兴趣的:(常用方法)