帮助C#Arraylist Itemlist.add显示在列表框中,使用for循环进行排序,使用foreach进行逆向排序? [英] help with C# Arraylist Itemlist.add to display in a listbox using for loop for sorted and foreach for reverse order?

查看:58
本文介绍了帮助C#Arraylist Itemlist.add显示在列表框中,使用for循环进行排序,使用foreach进行逆向排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 public partial class Form1 : Form
    {
        Array Arraylist();//Error	4	' declare a body because it is not marked abstract, extern, or partial	

    int i = 0;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
       //	items.Sort(); 
	 ItemList.Sort();
     ItemList.Add("item1");
     ItemList.Add("Item2");
     ItemList.Add("Item3");
     ItemList.Add("Item4");
     ItemList.Add("Item5");
     for (i = 0; i < (ItemList.Count - 1); i++) ;
            lilsorted= ItemList.Sort();
            //items.reverse
     ItemList.Reverse();
     ItemList.Add("item1");
     ItemList.Add("Item2");
     ItemList.Add("Item3");
     ItemList.Add("Item4");
     ItemList.Add("Item5");
            foreach (ItemList.Reverse <  lilreverse.Text>));//Error	1	Idreversexpected	

             lilreverse = ItemList.Reverse();
        }
   
        private void button2_Click(object sender, EventArgs e)
        {
            item1.Text = ("");
   Item3    item2.Text = ("");
  Item4     item3.Text = ("");
 Item5      item4.Text = ("");
            item5.Text = ("");
   reverseilsorted.Text = ("");
            lilreversebutton ("");
        }
 

        private void button3_Click(object sender, EventArgs e)
      Arraylist       this.Close();
        }
    
public  ArrayList ItemList { get; set; }}


}

推荐答案

我没有得到您确切要求的内容,您能解释一下吗?

如果要对arraylist进行排序和反向,则可以使用Sort()和Reverse()方法.
选中此链接 [
I did not got what exactly you are asking for, can you explain more abt it.

If you want to sort and reverse arraylist, you can use method Sort() and Reverse().
Check this link[^]


我认为通用列表很容易用于此目的,如下所示

I think generic list is easy to use for this purpose as shown below

void Main()
{
	Form form1 = new Form();
	ListBox listBox1 = new ListBox();
	listBox1.Dock= DockStyle.Fill;
	form1.Controls.Add(listBox1);
	
	List<string> items = new List<string>{"Item5","Item2","Item4","Item1","Item3"};
	//Option1
	items.Sort((s1,s2)=>s2.CompareTo(s1));
	//Option2
//	items.Sort();
//	items.Reverse();
	
	listBox1.DataSource=items;
	form1.ShowDialog();
	
}


首先,我将使用通用List<string>而不是非通用Array.除非您将列表用于除字符串之外的其他内容,否则将通过使用泛型来加快处理速度,并为您提供更大的灵活性.然后可以使用列表排序功能进行排序:

First of all I would use the generic List<string> instead of non-generic Array. Unless you are going to be using the list for something besides string, you will speed things up by using generic and gives you more flexibility. Sorting can then be done with the List sort capability:

Array.Sort(delegate(string s1, string s2) 
    { 
      return s2.CompareTo(s1); 
    });



解决方案2是正确的,因为您可以执行排序和反转",但这是ArrayListList<t></t>类,并且您将被限制为标准的sort \.您可以使用Array.Reverse(ArrayList)(排序也是静态方法).



Solution 2 is correct in that you can do a Sort and Reverse, but that is an ArrayList or List<t></t> class, and you would be limited to the standard sort \. There is a Array.Reverse (ArrayList) that you can use (the sort is also a static method).


这篇关于帮助C#Arraylist Itemlist.add显示在列表框中,使用for循环进行排序,使用foreach进行逆向排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆