文字と数字を含むリストボックスアイテムをソートしようとしています。例えば、当社の商品は以下のとおりです。リストボックスアイテムを数字で並べ替え
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);
}
}
のようなものは、項目の文字列ですか?あなたはそれのためのオブジェクトを持っていますか? –
文字列を処理し、数値を分割し、それらを適切にソートするために整数に変換する必要があります。 – axlj
あなたの質問はどこですか? –