3 回答
TA贡献1831条经验 获得超9个赞
最好Dictionary用他的汇率存储银行名称
var dict = new Dictionary<string, string>();
dict.Add("HSBC","1.58%");
//so on
接下来,要订购费率,您只需按价值订购
var dictOrdered = dict.OrderByDescending(x=> x.Value);
然后将每个项目添加Dictionary到ListBox
foreach(KeyValuePair<string, string> entry in dictOrdered)
{
listBox1.Items.Add($"{entry.Key} \t\t {entry.Value}");
}
这适用于您的情况,通过比较string,但通常是正确的方法:您必须double在比较之前将值转换为
//highest to low
var dict = new Dictionary<string, string>();
dict.Add("HSBC","1.58%");
var dictOrdered = dict.OrderByDescending(x=> double.Parse(x.Value.TrimEnd( new char[] { '%' })));
TA贡献1858条经验 获得超8个赞
您可以采取的解决此问题的方法是:
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);
}
}
}
- 3 回答
- 0 关注
- 142 浏览
添加回答
举报