按内部数字对列表框项目进行排序

时间:2016-12-08 21:13:21

标签: c#

我正在尝试对包含字母和数字的ListBox项进行排序。例如,我们的项目是:

London 4, 
Berlin 6, 
Paris 2, 
Roma 7, 
Istanbul 5.

排序代码将返回给我们:

Roma 7, 
Berlin 6, 
Istanbul 5, 
London 4, 
Paris 2. 

这是我的代码:

private void btnSearch_Click(object sender, EventArgs e)
{
    try
    {
        txtSearch.Text.Trim();
        if (!string.IsNullOrEmpty(txtSearch.Text))
        {
            txtSearch.Text = txtSearch.Text.Substring(0, 1).ToUpper() + txtSearch.Text.Substring(1, txtSearch.Text.Length - 1).ToLower();
            foreach (var item in lstCities.Items)
            {
                if (txtSearch.Text == item.ToString())
                {
                    lstRightSearches.Items.Add(txtSearch.Text);
                    txtSearch.Clear();
                }
            }
            if (!string.IsNullOrEmpty(txtSearch.Text))
            {
                if (lstWrongSearches.Items.Count > 0)
                {
                    foreach (var item in lstWrongSearches.Items)
                    {
                        string[] strArray = item.ToString().Split(' ');
                        if (txtSearch.Text == strArray[0].ToString())
                        {
                            strArray[1] = (Convert.ToUInt32(strArray[1].ToString()) + 1).ToString();
                            int index = lstWrongSearches.Items.IndexOf(item);
                            lstWrongSearches.Items.Remove(item);
                            lstWrongSearches.Items.Insert(index, strArray[0].ToString() + " " + strArray[1].ToString());
                            txtSearch.Clear();
                            break;
                        }
                    }
                    if (!string.IsNullOrEmpty(txtSearch.Text))
                    {
                        lstWrongSearches.Items.Add(txtSearch.Text + " 1");
                        txtSearch.Clear();
                    }
                }

                    ArrayList sortedArray = new ArrayList();
                    foreach (var item in lstWrongSearches.Items)
                    {
                        sortedArray.Add(item);
                    }

                    sortedArray.Sort();
                    lstWrongSearches.Items.Clear();
                    foreach (var item in sortedArray)
                    {
                        lstWrongSearches.Items.Add(item);
                    }
                else
                {
                    lstWrongSearches.Items.Add(txtSearch.Text + " 1");
                    txtSearch.Clear();
                }
            }
            txtSearch.Focus();
        }
        else
        {
            MessageBox.Show("Aradığınız Şehir?");
        }
    }
    catch (Exception Ex)
    {
        MessageBox.Show("Error in Adding ListBoxes" + Ex);
    }
}

2 个答案:

答案 0 :(得分:4)

你为什么不使用linq?

之类的东西
var cities = new List<string>() { "London 4", "Berlin 6", "Paris 2", "Roma 7", "Istanbul 5" };

var orderedCities = cities.OrderByDescending(c => int.Parce(c.Substring(c.LastIndexOf(" ")));

答案 1 :(得分:4)


List<string> strings = new List<string>(new[]
{
   "London 4",
   "Berlin 6", 
   "Paris 2",
   "Roma 7",
   "Istanbul 5"
});

var result = strings.OrderByDescending(s => int.Parse(s.Split()[1]));