按升序排列列表框

时间:2018-08-03 09:46:43

标签: c# arrays listbox

大家好,我是c#的新手,我真的可以在这里使用一些帮助。我希望按照此picture link

中的最高到最低,反之亦然(它们是利率)来排列列表框中的项目。

我尝试了使用数组的多种方法,但是似乎都没有用。我可以帮忙。

我当前的参考代码:

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)
        {




        }
    }
}

4 个答案:

答案 0 :(得分:1)

最好使用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[] { '%' })));

答案 1 :(得分:0)

按照添加项目的顺序显示项目,因此您应该先进行订购,然后再按该顺序添加项目。

答案 2 :(得分:0)

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

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)

添加一个额外的类来表示您的BankRate数据,并为简单起见覆盖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},
};

方法1:1行

// 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)

Methode2: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());
}