3 回答

TA貢獻1831條經(jīng)驗 獲得超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在比較之前將值轉(zhuǎn)換為
//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條經(jīng)驗 獲得超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 關(guān)注
- 171 瀏覽
添加回答
舉報