In this example, we’ll learn how to sort ListBox Items in descending (or decreasing) order using ArrayList.
Source Code:
1 2 3 |
using System.Collections; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
private void Form1_Load(object sender, EventArgs e) { listBox1.Items.Add("C#"); listBox1.Items.Add("Java"); listBox1.Items.Add("C++"); listBox1.Items.Add("Python"); listBox1.Items.Add("Pascal"); listBox1.Items.Add("Delphi"); listBox1.Items.Add("Visual Basic"); } private void button1_Click(object sender, EventArgs e) { ArrayList list = new ArrayList(); foreach(object o in listBox1.Items) { list.Add(o); } list.Sort(); list.Reverse(); listBox1.Items.Clear(); foreach (object o in list) { listBox1.Items.Add(o); } } |
YouTube