C# 按升序排列列表框

C# 按升序排列列表框,c#,arrays,listbox,C#,Arrays,Listbox,嗨,伙计们,我是c#的新手,我真的需要一些帮助。我希望按照从高到低的顺序排列列表框中的项目,反之亦然(它们是利率),如图所示 我尝试了多种使用数组的方法,但似乎都不起作用。我需要一些帮助 我目前的代码供参考: namespace Group_Project_Final { public partial class Form2 : Form { public Form2() { InitializeComponent();

嗨,伙计们,我是c#的新手,我真的需要一些帮助。我希望按照从高到低的顺序排列列表框中的项目,反之亦然(它们是利率),如图所示

我尝试了多种使用数组的方法,但似乎都不起作用。我需要一些帮助

我目前的代码供参考:

namespace Group_Project_Final
{
    public partial class Form2 : Form
    {


        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string DBS, OCBC, UOB, MayBank, HSBC, RHB;
            DBS = "1.60%";
            OCBC = "1.65%";
            MayBank = "1.62%";
            UOB = "1.55%";
            RHB = "1.68%";
            HSBC = "1.58%";
            listBox1.Items.Clear();
            listBox1.Items.Add("Bank\t\tRates");
            listBox1.Items.Add("DBS" + "\t\t" + DBS );
            listBox1.Items.Add("OCBC" + "\t\t" + OCBC);
            listBox1.Items.Add("HSBC" + "\t\t" + HSBC);
            listBox1.Items.Add("RHB" + "\t\t" + RHB);
            listBox1.Items.Add("UOB" + "\t\t" + UOB);
            listBox1.Items.Add("May Bank" + "\t" + MayBank);

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {




        }
    }
}

项目按您添加它们的顺序显示,因此您应该先进行排序,然后再按该顺序添加

最好使用
字典
来存储银行名称和他的利率

var dict = new Dictionary<string, string>();
dict.Add("HSBC","1.58%");
//so on
然后将
字典
中的每个项目添加到
列表框

    foreach(KeyValuePair<string, string> entry in dictOrdered)
    {
        listBox1.Items.Add($"{entry.Key} \t\t {entry.Value}");
    }

您可以采取的解决此问题的方法是:

namespace Group_Project_Final
{
    public partial class Form2 : Form
    {
        Dictionary<string, double> interestRates;

        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            interestRates = new Dictionary<string, double>();
            interestRates.Add("DBS", 1.60);
            interestRates.Add("OCBC", 1.65);
            interestRates.Add("UOB", 1.55);
            interestRates.Add("May Bank", 1.62);
            interestRates.Add("HSBC", 1.58);
            interestRates.Add("RHB", 1.68);

            listBox1.Items.Clear();
            listBox1.Items.Add("Bank\t\tRates");
            foreach(KeyValuePair<string, double> entry in interestRates)
            {
                listbox1.Items.Add($"{entry.Key}\t\t{entry.Value:0.##}%");
            }

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            //order interest rates either from high to low (descending)
            interestRates.OrderByDescending(item => item.Value);
            //or from low to high
            interestRates.OrderBy(item => item.Value);
        }
    }
}
名称空间组\u项目\u最终版本
{
公共部分类表单2:表单
{
字典利率;
公共表格2()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
interestRates=新字典();
增加(“DBS”,1.60);
增加(“OCBC”,1.65);
增加(“UOB”,1.55);
加上(“May Bank”,1.62);
利息。添加(“汇丰”,1.58);
增加(“RHB”,1.68);
listBox1.Items.Clear();
列表框1.Items.Add(“银行交易”);
foreach(利率中的KeyValuePair条目)
{
listbox1.Items.Add($“{entry.Key}\t\t{entry.Value:0.##}%”);
}
}
私有无效复选框1\u CheckedChanged(对象发送方,事件参数e)
{
//将利率从高到低排序(降序)
interestRates.OrderByDescending(item=>item.Value);
//还是从低到高
interestRates.OrderBy(item=>item.Value);
}
}
}

添加一个表示银行利率数据的额外类,并覆盖ToString()以简化操作

public class bRate
{
    public int Id;
    public string Name;
    public double Percent;

    public override string ToString()
    {
        return $"{Id}: {Name}\t\t{Percent}";
    }

}
在您的活动中,您可以使用以下方法之一。 第一个是展示一个外类的功能和简单性。 第二项正是您需要的,它将添加您需要的默认项

//Creating the list or reading it from database
var inputs = new List<bRate> {
    new bRate {Id = 1, Name= "DBS" , Percent=1.60},
    new bRate {Id = 2, Name= "OCBC", Percent=1.65},
    new bRate {Id = 3, Name= "UOB" , Percent=1.62},
};
方法2:3行

// Add the First default item
myListBox2.Items.Add("Bank\t\tRates");
Add all the other
foreach (var item in inputs.OrderBy(x => x.Percent)) {
    myListBox2.Items.Add(item.ToString());
}

您是否不想将列表框绑定到一个列表对象自定义对象,而不想拥有一些属性,如(id、名称、评级),并选择显示哪些属性和启用哪些属性进行筛选。@Dragandhi这是一个好主意!有没有关于我如何做到这一点的建议?
//Creating the list or reading it from database
var inputs = new List<bRate> {
    new bRate {Id = 1, Name= "DBS" , Percent=1.60},
    new bRate {Id = 2, Name= "OCBC", Percent=1.65},
    new bRate {Id = 3, Name= "UOB" , Percent=1.62},
};
// Order the list base on the object property
// Simple list bind to ListBox and string overwrite
myListBox1.dataSource = inputs.OrderBy(x => x.Percent);

// exemple of ordering by Percent then by name if some bank have the same rate.
// inputs.OrderBy(x => x.Percent).ThenBy(x => x.Name)
// Add the First default item
myListBox2.Items.Add("Bank\t\tRates");
Add all the other
foreach (var item in inputs.OrderBy(x => x.Percent)) {
    myListBox2.Items.Add(item.ToString());
}