c# - List<string> 自定义排序

标签 c# asp.net list sorting

我目前有一个 List<string>存储以下 HTML 字符串的对象,主要是 URL,例如

List[0] = "<a href='#' id='1'>Banana</a>";
List[1] = "<a href='#' id='2'>Orange</a>";
List[2] = "<a href='#' id='3'>Apple</a>";
List[3] = "<a href='#' id='4'>Mango</a>";
...

而且,当我对 List<string> 进行排序时对象,通过做URLList.Sort(); (assume that URLList object has been instantiated.) ,它给出了这样的顺序,Banana=>Orange=>Apple=>Mango,这并不是我想要的。

我想根据这些水果的字母顺序对其进行排序,例如Apple=>Banana=>Mango=>Orange

那么如何实现这种自定义排序呢?有什么建议么?谢谢。

最佳答案

如果你想要一个简单的方法,并且你希望你的数组不会改变太多:

2012 年 11 月 30 日编辑

List.Sort((x, y) => x.Substring(x.IndexOf(">") + 1, 1).CompareTo(y.Substring(y.IndexOf(">") + 1, 1)));

enter image description here

关于c# - List<string> 自定义排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13638346/

相关文章:

c# - 在 Windows 应用商店应用程序 (Windows 8.1) 中使用 GeoLocator 时出现 "The pipe is being closed"错误?

c# - 解析 "*"- 量词 {x,y} 没有任何内容

javascript - 从 RadDateTimePicker 中扣除天数

c# - 在 Web api Controller 中使用身份验证过滤器发送 json 响应?

java - 比较 Hashmap 和 List list = new Arraylist()

c# - 如何减少 'foreach' 中的相同字符串?

c# - 我应该如何在 C# 中设计字符串验证类?

c# - 如何在Knockoutjs中访问asp.net表单服务器端变量

仅保留所有矩阵中存在的行

python - 在 python 中输出列表