按升序排列列表框

大家好,我是 C# 新手,我真的可以在这里使用一些帮助。我希望按照从高到低的顺序排列我的列表框中的项目,反之亦然(它们是利率)

http://img2.mukewang.com/6190af5a0001e8ae19141075.jpg

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

我目前的代码供参考:


白衣非少年
浏览 143回答 3
3回答

天涯尽头无女友

最好Dictionary用他的汇率存储银行名称var dict = new Dictionary<string, string>();dict.Add("HSBC","1.58%");//so on接下来,要订购费率,您只需按价值订购var dictOrdered = dict.OrderByDescending(x=> x.Value);然后将每个项目添加Dictionary到ListBox&nbsp; &nbsp; foreach(KeyValuePair<string, string> entry in dictOrdered)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; listBox1.Items.Add($"{entry.Key} \t\t {entry.Value}");&nbsp; &nbsp; }这适用于您的情况,通过比较string,但通常是正确的方法:您必须double在比较之前将值转换为&nbsp; &nbsp; //highest to low&nbsp; &nbsp; var dict = new Dictionary<string, string>();dict.Add("HSBC","1.58%");&nbsp; &nbsp; var dictOrdered = dict.OrderByDescending(x=> double.Parse(x.Value.TrimEnd( new char[] { '%' })));

猛跑小猪

您可以采取的解决此问题的方法是:namespace Group_Project_Final{&nbsp; &nbsp; public partial class Form2 : Form&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Dictionary<string, double> interestRates;&nbsp; &nbsp; &nbsp; &nbsp; public Form2()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; private void button1_Click(object sender, EventArgs e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interestRates = new Dictionary<string, double>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interestRates.Add("DBS", 1.60);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interestRates.Add("OCBC", 1.65);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interestRates.Add("UOB", 1.55);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interestRates.Add("May Bank", 1.62);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interestRates.Add("HSBC", 1.58);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interestRates.Add("RHB", 1.68);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listBox1.Items.Clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listBox1.Items.Add("Bank\t\tRates");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach(KeyValuePair<string, double> entry in interestRates)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listbox1.Items.Add($"{entry.Key}\t\t{entry.Value:0.##}%");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; private void checkBox1_CheckedChanged(object sender, EventArgs e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //order interest rates either from high to low (descending)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interestRates.OrderByDescending(item => item.Value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //or from low to high&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interestRates.OrderBy(item => item.Value);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

泛舟湖上清波郎朗

项目按您添加的顺序显示,因此您应该先订购,然后按该顺序添加。
打开App,查看更多内容
随时随地看视频慕课网APP