赞
踩
一、概述
SortedList 类代表了一系列按照键来排序的键/值对,这些键值对可以通过键和索引来访问。
排序列表是数组和哈希表的组合。它包含一个可使用键或索引访问各项的列表。
如果您使用索引访问各项,则它是一个动态数组(ArrayList).
如果您使用键访问各项,则它是一个哈希表(Hashtable)。集合中的各项总是按键值排序。
二、SortedList 类的方法和属性
常用的属性
属性 描述
Capacity 获取或设置 SortedList 的容量。
Count 获取 SortedList 中的元素个数。
IsFixedSize 获取一个值,表示 SortedList 是否具有固定大小。
IsReadOnly 获取一个值,表示 SortedList 是否只读。
Item 获取或设置与 SortedList 中指定的键相关的值。
Keys 获取 SortedList 中的键。
Values 获取 SortedList 中的值。
三、常用的方法
序号 方法名 描述
1 public virtual void Add( object key, object value ); 向 SortedList 添加一个带有指定的键和值的元素。
2 public virtual void Clear(); 从 SortedList 中移除所有的元素。
3 public virtual bool ContainsKey( object key ); 判断 SortedList 是否包含指定的键。
4 public virtual bool ContainsValue( object value ); 判断 SortedList 是否包含指定的值。
5 public virtual object GetByIndex( int index ); 获取 SortedList 的指定索引处的值。
6 public virtual object GetKey( int index ); 获取 SortedList 的指定索引处的键。
7 public virtual IList GetKeyList(); 获取 SortedList 中的键。
8 public virtual IList GetValueList(); 获取 SortedList 中的值。
9 public virtual int IndexOfKey( object key ); 返回 SortedList 中的指定键的索引,索引从零开始。
10 public virtual int IndexOfValue( object value ); 返回 SortedList 中的指定值第一次出现的索引,索引从零开始。
11 public virtual void Remove( object key ); 从 SortedList 中移除带有指定的键的元素。
12 public virtual void RemoveAt( int index ); 移除 SortedList 的指定索引处的元素。
13 public virtual void TrimToSize(); 设置容量为 SortedList 中元素的实际个数。
四、实例
using System;
using System.Collections;
namespace CollectionsApplication{
class Program{
static void Main(string[] args){
SortedList sl = new SortedList();
sl.Add("001", "Zara Ali");
sl.Add("002", "Abida Rehman");
sl.Add("003", "Joe Holzner");
sl.Add("004", "Mausam Benazir Nur");
sl.Add("005", "M. Amlan");
sl.Add("006", "M. Arif");
sl.Add("007", "Ritesh Saikia");
if (sl.ContainsValue("Nuha Ali"))
{
Console.WriteLine("This student name is already in the list");
}
else
{
sl.Add("008", "Nuha Ali");
}
// 获取键的集合
ICollection key = sl.Keys;
foreach (string k in key)
{
Console.WriteLine(k + ": " + sl[k]);
}
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
001: Zara Ali
002: Abida Rehman
003: Joe Holzner
004: Mausam Banazir Nur
005: M. Amlan
006: M. Arif
007: Ritesh Saikia
008: Nuha Ali
五、Sort
Sort 是 List<T> 自带的排序方法,其排序特性如下:
1、默认升序;
2、会改变原列表;
3、不稳定排序:相等元素的相对次序可能会改变。
方法一:常规
List<int> intList = new List<int> {4, 5, 1, 3, 2};
// 升序
intList.Sort(); // 1 2 3 4 5
// 降序(先升序,再反转)
intList.Sort();
intList.Reverse(); // 5 4 3 2 1
方法二:CompareTo
List<int> intList = new List<int> {4, 5, 1, 3, 2};
// 升序
intList.Sort((x, y) => x.CompareTo(y)); // 1 2 3 4 5
// 降序
intList.Sort((x, y) => y.CompareTo(x)); // 5 4 3 2 1
六、示例
自定义 Student 类
public class Student
{
public string name {get; set;}
public int age {get; set;}
public int score {get; set;}
}
单属性排序
void dumpList(List<Student> stuList, string desc)
{
Console.WriteLine(desc);
foreach(var student in stuList)
{
Console.Write("name = {0}, age = {1}, score = {2}", student.name, student.age, student.score);
Console.WriteLine();
}
}
List<Student> stuList = new List<Student> {
new Student() {name = "张三", age = 20, score = 95},
new Student() {name = "李四", age = 19, score = 99},
new Student() {name = "王五", age = 21, score = 95},
new Student() {name = "赵六", age = 20, score = 90},
new Student() {name = "陈七", age = 22, score = 95},
new Student() {name = "刘八", age = 21, score = 92}
};
dumpList(stuList, "-----原始列表-----");
// -----原始列表-----
// name = 张三, age = 20, score = 95
// name = 李四, age = 19, score = 99
// name = 王五, age = 21, score = 95
// name = 赵六, age = 20, score = 90
// name = 陈七, age = 22, score = 95
// name = 刘八, age = 21, score = 92
stuList.Sort((x, y) => x.score.CompareTo(y.score));
dumpList(stuList, "-----按 score 属性升序-----");
// -----按 score 属性升序-----
// name = 赵六, age = 20, score = 90
// name = 刘八, age = 21, score = 92
// name = 张三, age = 20, score = 95
// name = 陈七, age = 22, score = 95
// name = 王五, age = 21, score = 95
// name = 李四, age = 19, score = 99
stuList.Sort((x, y) => y.age.CompareTo(x.age));
dumpList(stuList, "-----按 age 属性降序-----");
// -----按 age 属性降序-----
// name = 陈七, age = 22, score = 95
// name = 王五, age = 21, score = 95
// name = 刘八, age = 21, score = 92
// name = 赵六, age = 20, score = 90
// name = 张三, age = 20, score = 95
// name = 李四, age = 19, score = 99
嵌套排序
List<Student> stuList = new List<Student> {
new Student() {name = "张三", age = 20, score = 95},
new Student() {name = "李四", age = 19, score = 99},
new Student() {name = "王五", age = 21, score = 95},
new Student() {name = "赵六", age = 20, score = 90},
new Student() {name = "陈七", age = 22, score = 95},
new Student() {name = "刘八", age = 21, score = 92}
};
dumpList(stuList, "-----原始列表-----");
// -----原始列表-----
// name = 张三, age = 20, score = 95
// name = 李四, age = 19, score = 99
// name = 王五, age = 21, score = 95
// name = 赵六, age = 20, score = 90
// name = 陈七, age = 22, score = 95
// name = 刘八, age = 21, score = 92
stuList.Sort((x, y) => {
int ret = y.score.CompareTo(x.score);
if (ret == 0)
{
return x.age.CompareTo(y.age);
}
else
{
return ret;
}
});
dumpList(stuList, "-----嵌套排序:按 score 降序,score 相同则按 age 升序-----");
// -----嵌套排序:按 score 降序,score 相同则按 age 升序-----
// name = 李四, age = 19, score = 99
// name = 张三, age = 20, score = 95
// name = 王五, age = 21, score = 95
// name = 陈七, age = 22, score = 95
// name = 刘八, age = 21, score = 92
// name = 赵六, age = 20, score = 90
ps:
obj1.CompareTo(obj2) 返回值(int):
大于0(1):obj1 在 obj2 后面;
等于0(0):obj1 和 obj2 在同一位置;
小于0(-1):obj1 在 obj2 前面;
七、OrderBy/OrderByDescending
OrderBy/OrderByDescending (OrderBy升序,OrderByDescending降序)是通用的序列排序方法,其排序特性如下:
1、使用前必须先引入命名空间:System.Linq;
2、不会改变原列表;
3、稳定排序:相等元素的相对次序不会改变。
基本数据类型
List<int> intList = new List<int> {4, 5, 1, 3, 2};
// 升序
intList = intList.OrderBy(x => x).ToList(); // 1 2 3 4 5
// 降序
intList = intList.OrderByDescending(x => x).ToList(); // 5 4 3 2 1
单属性排序
List<Student> stuList = new List<Student> {
new Student() {name = "张三", age = 20, score = 95},
new Student() {name = "李四", age = 19, score = 99},
new Student() {name = "王五", age = 21, score = 95},
new Student() {name = "赵六", age = 20, score = 90},
new Student() {name = "陈七", age = 22, score = 95},
new Student() {name = "刘八", age = 21, score = 92}
};
dumpList(stuList, "-----原始列表-----");
// -----原始列表-----
// name = 张三, age = 20, score = 95
// name = 李四, age = 19, score = 99
// name = 王五, age = 21, score = 95
// name = 赵六, age = 20, score = 90
// name = 陈七, age = 22, score = 95
// name = 刘八, age = 21, score = 92
stuList = stuList.OrderBy(obj => obj.score).ToList();
dumpList(stuList, "-----按 score 属性升序-----");
// -----按 score 属性升序-----
// name = 赵六, age = 20, score = 90
// name = 刘八, age = 21, score = 92
// name = 张三, age = 20, score = 95
// name = 王五, age = 21, score = 95
// name = 陈七, age = 22, score = 95
// name = 李四, age = 19, score = 99
stuList = stuList.OrderByDescending(obj => obj.age).ToList();
dumpList(stuList, "-----按 age 属性降序-----");
// -----按 age 属性降序-----
// name = 陈七, age = 22, score = 95
// name = 刘八, age = 21, score = 92
// name = 王五, age = 21, score = 95
// name = 赵六, age = 20, score = 90
// name = 张三, age = 20, score = 95
// name = 李四, age = 19, score = 99
嵌套排序
List<Student> stuList = new List<Student> {
new Student() {name = "张三", age = 20, score = 95},
new Student() {name = "李四", age = 19, score = 99},
new Student() {name = "王五", age = 21, score = 95},
new Student() {name = "赵六", age = 20, score = 90},
new Student() {name = "陈七", age = 22, score = 95},
new Student() {name = "刘八", age = 21, score = 92}
};
dumpList(stuList, "-----原始列表-----");
// -----原始列表-----
// name = 张三, age = 20, score = 95
// name = 李四, age = 19, score = 99
// name = 王五, age = 21, score = 95
// name = 赵六, age = 20, score = 90
// name = 陈七, age = 22, score = 95
// name = 刘八, age = 21, score = 92
stuList = stuList.OrderByDescending(obj => obj.score).ThenBy(obj => obj.age).ToList();
dumpList(stuList, "-----嵌套排序:按 score 降序,score 相同则按 age 升序-----");
// -----嵌套排序:按 score 降序,score 相同则按 age 升序-----
// name = 李四, age = 19, score = 99
// name = 张三, age = 20, score = 95
// name = 王五, age = 21, score = 95
// name = 陈七, age = 22, score = 95
// name = 刘八, age = 21, score = 92
// name = 赵六, age = 20, score = 90
stuList = stuList.OrderBy(obj => obj.score).ThenByDescending(obj => obj.age).ToList();
dumpList(stuList, "-----嵌套排序:按 score 升序,score 相同则按 age 降序-----");
// -----嵌套排序:按 score 升序,score 相同则按 age 降序-----
// name = 赵六, age = 20, score = 90
// name = 刘八, age = 21, score = 92
// name = 陈七, age = 22, score = 95
// name = 王五, age = 21, score = 95
// name = 张三, age = 20, score = 95
// name = 李四, age = 19, score = 99
ps:
OrderBy/OrderByDescending 返回序列的类型为 IOrderedEnumerable<TSource>,可以用 ToList 将其转为列表!
即:
stuList = stuList.OrderBy(obj => obj.score).ToList();
=>
IOrderedEnumerable<Student> query = stuList.OrderBy(obj => obj.score);
stuList = query.ToList();
————————————————
版权声明:本文为CSDN博主「幸福清风」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xun527/article/details/124465628
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。