[英]List<string> Custom Sorting
I currently have a List<string>
object that stores following HTML strings, mainly the URLs, eg 我目前有一个
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>";
...
And, when I sort the List<string>
object, by doing URLList.Sort(); (assume that
并且,当我对
List<string>
对象进行排序时,通过执行URLList.Sort(); (assume that
URLList.Sort(); (assume that
URLList object has been instantiated.)
, it gives such order, Banana=>Orange=>Apple=>Mango , which is not exactly what I wanted. URLList.Sort(); (assume that
object has been instantiated.)
URLList.Sort(); (assume that
URLList object has been instantiated.)
,它给出了这样的顺序, Banana => Orange => Apple => Mango ,这不是我想要的。
I'd like to have it sorted based on the alphabetical order of these fruit, which is like Apple=>Banana=>Mango=>Orange . 我想根据这些水果的字母顺序排序,就像Apple => Banana => Mango => Orange 。
So how can I achieve this custom sorting? 那么我该如何实现这种自定义排序呢? Any suggestions?
有什么建议? Thanks.
谢谢。
If you want a simple way of doing it, and you expect your array to not change too much: 如果你想要一个简单的方法,你希望你的数组不会改变太多:
EDITED 11/30/2012 2012年11月30日编辑
List.Sort((x, y) => x.Substring(x.IndexOf(">") + 1, 1).CompareTo(y.Substring(y.IndexOf(">") + 1, 1)));
I'd consider turning your List<string>
into a List<KeyValuePair<string, string>>
. 我会考虑将
List<string>
转换为List<KeyValuePair<string, string>>
。 You can populate it with the Key
being set to "Banana", "Apple", etc. and the Value
being the url itself. 你可以使用
Key
设置为“Banana”,“Apple”等来填充它,并且Value
是url本身。 Then you can sort on the Key
. 然后你可以对
Key
进行排序。
Default sorting for strings is alphabetical , which in this case is controlled by the id
attribute, since that is the first part of the strings that is different . 字符串的 默认排序是按字母顺序排列的 ,在这种情况下由
id
属性控制,因为这是不同字符串的第一部分 。 You would want to use the version of sort which takes a comparison object and provide your own comparison function which correctly parses your tag and returns the order. 您可能希望使用带有比较对象的sort版本,并提供您自己的比较函数 ,该函数正确地解析您的标记并返回订单。 Alternatively read your HTML tag into a class, and store it as a list of these objects, would make the code more readable.
或者,将HTML标记读入类中,并将其存储为这些对象的列表,这样可以使代码更具可读性。
This is expected. 这是预料之中的。 You are sorting simple strings and the first place they vary is at the value of the
id
attribute. 您正在对简单字符串进行排序,它们变化的第一个位置是
id
属性的值。
I assume it's calling ToString()
to determine the sort order, so you could try overriding that to return only the inner text. 我假设它调用
ToString()
来确定排序顺序,因此您可以尝试重写它以仅返回内部文本。 But I really think you need a class here instead of just strings. 但我真的认为你需要一个班级,而不仅仅是字符串。
I'm new to C# and ASP.NET, but maybe you could use a System.Web.UI.WebControls.HyperLink
instead of a string: 我是C#和ASP.NET的新手,但也许您可以使用
System.Web.UI.WebControls.HyperLink
而不是字符串:
List<HyperLink> links = new List<HyperLink>();
links.Add(new HyperLink() { NavigateURL = "#", ID = "1", Text = "Banana" });
links.Add(new HyperLink() { NavigateURL = "#", ID = "2", Text = "Apple" });
links.Add(new HyperLink() { NavigateURL = "#", ID = "3", Text = "Orange" });
links.Add(new HyperLink() { NavigateURL = "#", ID = "4", Text = "Mango" });
links.Sort((x, y) => x.Text.CompareTo(y.Text));
These links can then be appended to another control on your page, such as a <div>
etc.: 然后可以将这些链接附加到页面上的另一个控件,例如
<div>
等:
Control parent = Page.FindControl("id-of-control");
foreach (HyperLink l in links)
parent.Controls.Add(l);
Because in your list, the string <a href='#' id='1'>Banana</a>
contains id
attribute, whose value is 1
, it's smaller than <a href='#' id='2'>Orange</a>
, whose id
value is 2
. 因为在您的列表中,字符串
<a href='#' id='1'>Banana</a>
包含id
属性,其值为1
,它小于<a href='#' id='2'>Orange</a>
,其id
值为2
。 So the result of URLList.Sort();
所以
URLList.Sort();
的结果URLList.Sort();
is correct. 是正确的。
I think you need to create your own String class, and override equals function, or implement Comparable
interface, then override compare
function. 我认为您需要创建自己的String类,并重写equals函数,或实现
Comparable
接口,然后重写compare
函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.