我编写了一个包含列表的代码,我希望按字母顺序和数字顺序对列表进行排序。
例如,列表中的第一项是
list[0] = "INPUT 10"
list[1] = "INPUT 5"
。我希望我的列表重组如下:
list[0] = "INPUT 5"
list[1] = "INPUT 10"
。 所以基本上我的程序从一个选中的列表框中获取检查项目,将它们存储在一个列表中,我希望它按字母顺序重新组织列表。
选中的列表框包含INPUT 1,INPUT 2,INPUT 3 ......等项目。任何人都可以建议我如何解决这个问题吗?
更新代码
我已经更新了我的代码,现在这段代码将字符串拆分为INPUT和10.“q”列表获取输入框中的选中项,字符串“s”数组从q列表中获取splittd数据。然后“数字”列表只获取字符串的数字部分,例如“INPUT 5”,数字列表将只获得“5”。然后我想对这些数字进行排序并构建另一个字符串,将排序的数字列表和字符串组合在一起“INPUT”并将其添加到输出checkedlistbox。我的代码不工作虽然...任何建议?它应该排序数字,但它没有...任何人有任何建议为什么这个代码不工作?而且我继续获取数组的错误消息,能够解除负整数和不能处理的错误。
List<string> q = new List<string>();
List<string> numbers = new List<string>();
private void button_ekle_Click(object sender, EventArgs e)
{
for (int k = clb_input.Items.Count - 1; k >= 0; k--)
{
if (clb_input.GetItemChecked(k) == true)
{
q.Add(clb_input.Items[k].ToString());
//clb_output.Items.Add(clb_input.Items[k]);
clb_input.Items.RemoveAt(k);
}
else { }
}
string[] s = new string[q.Count * 2];
//string[] numbers=new string[q.Count/2];
for (int t = 1; t <= q.Count * 2; t++)
{
if (q != null)
s = q[t - 1].ToString().Split(' ');
else { s[t] = null; }
}
for (int x = 1; x <= q.Count; x++)
{
if (s[2 * x - 1] != null)
{
numbers[x - 1] = s[2 * x - 1];
numbers.Sort();
clb_output.Items.Add("INPUT "+ numbers[x - 1].ToString());
}
else { numbers[x - 1] = null; }
}
}
答案 0 :(得分:3)
您需要的是Alphanumeric Sorting
(在Windows资源管理器中最常见,文件的排序方式)
可以在此处找到代码:http://www.dotnetperls.com/alphanumeric-sorting
样品
class Program
{
static void Main()
{
string[] highways = new string[]
{
"100F",
"50F",
"SR100",
"SR9"
};
//
// We want to sort a string array called highways in an
// alphanumeric way. Call the static Array.Sort method.
//
Array.Sort(highways, new AlphanumComparatorFast());
//
// Display the results
//
foreach (string h in highways)
{
Console.WriteLine(h);
}
}
}
输出
50F
100F
SR9
SR100
实施
public class AlphanumComparatorFast : IComparer
{
public int Compare(object x, object y)
{
string s1 = x as string;
if (s1 == null)
{
return 0;
}
string s2 = y as string;
if (s2 == null)
{
return 0;
}
int len1 = s1.Length;
int len2 = s2.Length;
int marker1 = 0;
int marker2 = 0;
// Walk through two the strings with two markers.
while (marker1 < len1 && marker2 < len2)
{
char ch1 = s1[marker1];
char ch2 = s2[marker2];
// Some buffers we can build up characters in for each chunk.
char[] space1 = new char[len1];
int loc1 = 0;
char[] space2 = new char[len2];
int loc2 = 0;
// Walk through all following characters that are digits or
// characters in BOTH strings starting at the appropriate marker.
// Collect char arrays.
do
{
space1[loc1++] = ch1;
marker1++;
if (marker1 < len1)
{
ch1 = s1[marker1];
}
else
{
break;
}
} while (char.IsDigit(ch1) == char.IsDigit(space1[0]));
do
{
space2[loc2++] = ch2;
marker2++;
if (marker2 < len2)
{
ch2 = s2[marker2];
}
else
{
break;
}
} while (char.IsDigit(ch2) == char.IsDigit(space2[0]));
// If we have collected numbers, compare them numerically.
// Otherwise, if we have strings, compare them alphabetically.
string str1 = new string(space1);
string str2 = new string(space2);
int result;
if (char.IsDigit(space1[0]) && char.IsDigit(space2[0]))
{
int thisNumericChunk = int.Parse(str1);
int thatNumericChunk = int.Parse(str2);
result = thisNumericChunk.CompareTo(thatNumericChunk);
}
else
{
result = str1.CompareTo(str2);
}
if (result != 0)
{
return result;
}
}
return len1 - len2;
}
}
答案 1 :(得分:0)
你可以像这样使用Sort with a Comparer:
List<string> q = new List<string>();
private void button_ekle_Click(object sender, EventArgs e)
{
for (int k=clb_input.Items.Count-1; k >= 0; k--)
{
if (clb_input.GetItemChecked(k) == true)
{
q.Add(clb_input.Items[k].ToString());
clb_input.Items.RemoveAt(k);
}
else { }
}
q.Sort((p1,p2)=>((int)(p1.split(" ")[1])).CompareTo((int)(p2.split(" ")[1])));
for (int t = 0; t < q.Count; t++)
{
clb_output.Items.Add(q[t].ToString());
}
}
答案 2 :(得分:0)
最简单的解决方案是用空格将数值左边填充到相同的长度。
List<string> lst = new List<string>
{
"Item 9",
"Item 999",
"Thing 999",
"Thing 5",
"Thing 1",
"Item 20",
"Item 10",
};
lst.Sort();
输出:
Item 9
Item 10
Item 20
Item 999
Thing 1
Thing 5
Thing 999
在执行排序操作后,您始终可以删除用于填充的额外空白区域。