我想从列表框中获取一堆项目,将它们添加到数组中,对其进行排序,然后将其放回到不同的列表框中。以下是我的想法:
ArrayList q = new ArrayList();
foreach (object o in listBox4.Items)
q.Add(o);
q.Sort();
listBox5.Items.Add(q.ToString());
但它不起作用。有什么想法吗?
答案 0 :(得分:19)
您可以使用ListBox.Sorted内置功能
foreach (object o in listBox4.Items)
{
listBox5.Items.Add(o);
}
listBox5.Sorted = true;
设置ListBox5.Sorted = true将确保列表框中的项目已排序,并且添加到列表框中的所有后续项目将按正确的顺序添加。
当然,这假设您的示例中提供了简单的排序要求。
答案 1 :(得分:13)
ArrayList q = new ArrayList();
foreach (object o in listBox4.Items)
q.Add(o);
}
q.Sort();
listBox5.Items.Clear();
foreach(object o in q){
listBox5.Items.Add(o);
}
答案 2 :(得分:9)
试试这个:
var list = lstBox.Items.Cast<ListItem>().OrderBy(item => item.Text).ToList();
lstBox.Items.Clear();
foreach (ListItem listItem in list)
{
lstBox.Items.Add(listItem);
}
如果您需要按值排序,只需使用item.Value切换item.Text。
享受!
答案 3 :(得分:1)
将项添加到数组并关闭循环。然后对数组值进行排序并将其绑定到列表框
答案 4 :(得分:1)
尝试AddRange
ArrayList q = new ArrayList();
foreach (object o in listBox4.Items)
q.Add(o);
q.Sort();
listBox5.Items.AddRange(q.ToArray());
答案 5 :(得分:0)
如果您正在使用.Net3.5使用linq来完成此任务。这里我使用列表进行转换和排序
var list = ListBox1.Items.Cast<ListItem>().Select(item => item.Value).ToList();
list.Sort();
ListBox2.DataSource =list;
ListBox2.DataBind();
答案 6 :(得分:0)
private void SortListBox(ListBox listBox)
{
SortedList<string, string> list = new SortedList<string, string>();
foreach (ListItem i in listBox.Items) {
list.Add(i.Text, i.Value);
}
listBox.Items.Clear();
foreach(KeyValuePair<string, string> i in list){
listBox.Items.Add(new ListItem(i.Key, i.Value));
}
}
答案 7 :(得分:0)
你也可以使用&#34;扩展方法&#34;我写道:
public static class ExtensionMethods
{
public static void Sort(this ListControl lb, bool desc = false)
{
var list = lb.Items.Cast<ListItem>().ToArray();
list = desc
? list.OrderByDescending(x => x.Text).ToArray()
: list.OrderBy(x => x.Text).ToArray();
lb.Items.Clear();
lb.Items.AddRange(list);
}
public static void SortByValue(this ListControl lb, bool desc = false)
{
var list = lb.Items.Cast<ListItem>().ToArray();
list = desc
? list.OrderByDescending(x => x.Value).ToArray()
: list.OrderBy(x => x.Value).ToArray();
lb.Items.Clear();
lb.Items.AddRange(list);
}
public static void SortByText(this ListControl lb, bool desc = false)
{
lb.Sort(desc);
}
public static void SortRandom(this ListControl lb)
{
var list = lb.Items.Cast<ListItem>()
.OrderBy(x => Guid.NewGuid().ToString())
.ToArray();
lb.Items.Clear();
lb.Items.AddRange(list);
}
}
答案 8 :(得分:0)
排序列表框描述
void sort()
{
if (listBox1.Items.Count <= 1)
return;
for (int j = 0; j < listBox1.Items.Count - 1; j++)
{
for (int i = 0; i < listBox1.Items.Count - 1; i++)
{
listBox1.SetSelected(i, true);
string a = listBox1.SelectedItem.ToString();
listBox1.SetSelected(++i, true);
i--;
string b = listBox1.SelectedItem.ToString();
if (b.CompareTo(a) == 1)
{
listBox1.Items.RemoveAt(i);
listBox1.Items.Insert(i, b);
i++;
listBox1.Items.RemoveAt(i);
listBox1.Items.Insert(i, a);
i--;
}
}
}
}
答案 9 :(得分:0)
protected void Sort(ListBox lbox)
{
try
{
List<KeyValuePair<string, string>> ListBoxList = new
List<KeyValuePair<string, string>>();
foreach (ListItem li in lbox.Items)
{
ListBoxList.Add(new KeyValuePair<string, string>(li.Value, li.Text));
}
if (ListBoxList.Count > 0)
{
ListBoxList = ListBoxList.OrderBy(x => x.Value).ToList();
lbox.DataTextField = "Value";
lbox.DataValueField = "Key";
lbox.DataSource = ListBoxList;
lbox.DataBind();
}
}
catch (Exception error)
{
error.WriteEvent();
throw;
}
}
答案 10 :(得分:0)
对于ASP.NET列表框:
private void SortListBox(ListBox oListBox)
{
ListBox oSortedListBox = new ListBox();
oSortedListBox.DataSource = oListBox.Items.Cast<ListItem>().ToDictionary(i => i.Value, i => i.Text).OrderBy(i => i.Value);
oSortedListBox.DataValueField = "Key";
oSortedListBox.DataTextField = "Value";
oSortedListBox.DataBind();
oListBox.Items.Clear();
foreach (ListItem oListItem in oSortedListBox.Items)
{
oListBox.Items.Add(oListItem);
}
}
答案 11 :(得分:0)
尝试以下操作,而不向任何数组添加元素
Listbox5.Items.AddRange(Listbox4.Items);
Listbox5.Sorted=true;