按文本而不是值对ArrayList项进行排序 [英] Sorting ArrayList Items by Text and not Value
问题描述
我正在对字符串值列表进行排序,但是我没有得到预期的结果.我去过Google,但运气不好.任何帮助都可以.
I''m sorting a list of string values but I''m not getting the expected results. I''ve been on Google and wrapped my head around with no luck. Any help would do please.
public void OrderDropDowns(ref DropDownList objDDL)
{
// ArrayList to keep text and value items within the specified Drop Down
ArrayList text_list = new ArrayList();
ArrayList value_list = new ArrayList();
foreach (ListItem li in objDDL.Items)
{
if (li.Text != "-Select One-")
{
text_list.Add(li.Text);
}
}
if (text_list.Count >= 8)
{
text_list.Sort(0, 4, null);
}
else
{
text_list.Sort();
}
foreach (object li in text_list)
{
string value = objDDL.Items.FindByText(li.ToString()).Value;
value_list.Add(value);
}
// Adding Sorted Items to the Drop Down List
objDDL.Items.Clear();
objDDL.Items.Add("-Select One-");
for (int i = 0; i < text_list.Count; i++)
{
if (i != text_list.Count)
{
ListItem objItem = new ListItem(text_list[i].ToString(), value_list[i].ToString());
objDDL.Items.Add(objItem);
}
}
}
值列表
例1年¥200000
例2岁50000
例1年60000日元
例2 Yr 150000
我需要对它进行排序
例1年60000日元
例1年¥200000
Ex 2 Yr 50000
例2 Yr 150000
我正在使用ArrayList Sort并将其排序为
例1年¥200000
例1年60000日元
例2岁50000
例2年150000
List of values
Ex 1 Yr 200000
Ex 2 Yr 50000
Ex 1 Yr 60000
Ex 2 Yr 150000
I need to sort it
Ex 1 Yr 60000
Ex 1 Yr 200000
Ex 2 Yr 50000
Ex 2 Yr 150000
I am using an ArrayList Sort and it sorting it as
Ex 1 Yr 200000
Ex 1 Yr 60000
Ex 2 Yr 50000
Ex 2 Yr 150000
推荐答案
从不使用ArrayList.使用诸如List< string>之类的通用类.另外,您最好使它成为一个结构.您要基于字符串中从位置9开始的数值进行排序.如果将它们保留为字符串,则需要编写一个自定义排序方法来执行此操作. 此处 [
Never use ArrayList. Use the generic classes like List<string>. In addition, you''d do better to make this a struct. You want to sort based on the numeric values starting at position 9 in your string. If you keep them as a string, you need to write a custom sort method to do this. Here[^] is an article on custom sorting. You want to compare the whole string first, THEN if the first sections are the same, parse out the number and compare it numerically.
通过IComparer实现使用替代排序方法.请参阅 MSDN [ http://en.csharp-online.net/CSharp_Generics_Recipes% E2%80%94Replacing_the_ArrayList_with_Its_Generic_Counterpart [ a>]),或 SortedList [
Use the alternate sorting method, by passing an IComparer implementation. See MSDN[^].
It is not a good approach to use two separated collections. Better create a class and use a generic list (http://en.csharp-online.net/CSharp_Generics_Recipes%E2%80%94Replacing_the_ArrayList_with_Its_Generic_Counterpart[^]), or a SortedList[^].
我希望您知道对字符串进行排序,您正在体验的输出正是预期的结果.
字符串"Ex 1 Yr 200000"小于"Ex 1 Yr 60000".控制此行为的适当术语称为整理顺序 [ IComparable [
I hope you are aware that the when one is sorting strings, the output you are experiencing is exactly what is to be expected.
The String "Ex 1 Yr 200000" is less than "Ex 1 Yr 60000". The proper term that governs this behaviour is called Collating Sequence[^].
From what you are expecting, you should actually pick apart your string into an appropriate type you will have to define, being careful to implement the IComparable[^] interface. This will let you control the way in which the elements will be sorted. When done with the sort you can reassemble the text strings in order to put them into your dropdown list.
Regards,
— Manfred
这篇关于按文本而不是值对ArrayList项进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!