我有一个列表框,我想从 a 到 z 对该列表框中的每个项目进行排序,并将这些代码分配到一个按钮中。我需要将数组分配给列表框吗?然后使用循环 ?
这是我做的:
protected void sortImageButton_Click(object sender, ImageClickEventArgs e)
{
string[] sort = new string[cartListBox.Items.Count];
for (int i = 0; i < sort.Length; i++)
{
sort[i] = cartListBox.Items[i].ToString();
Array.Sort(sort);
}
}
但是,当我点击按钮时,它什么也没做。
请您参考如下方法:
您需要在循环外对其进行排序。
protected void sortImageButton_Click(object sender, ImageClickEventArgs e)
{
string[] sort = new string[cartListBox.Items.Count];
for (int i = 0; i < sort.Length; i++)
{
sort[i] = cartListBox.Items[i].ToString();
}
Array.Sort(sort);
for (int i = 0; i < sort.Length; i++)
{
// reset the order for the cartListBox collection according to the sort array, if needed
}
}
