按字母顺序和数字顺序排序列表

本文关键字:顺序 排序 列表 数字 | 更新日期: 2023-09-27 18:17:55

我已经写了一个代码,有一个列表,我想排序的列表按字母顺序和数字。

例如,列表中的第一项是

  • list[0] = "INPUT 10"
  • list[1] = "INPUT 5" .

我希望我的列表像这样重组:

  • list[0] = "INPUT 5"
  • list[1] = "INPUT 10"

基本上我的程序从复选列表框中获取选中的项,将它们存储在一个列表中,我希望它按字母顺序重新组织列表。

复选框中有INPUT 1,INPUT 2,INPUT 3…这是第四个。有谁能给我一个建议吗?

<

更新代码/strong>

我已经更新了我的代码,现在这段代码将字符串分成INPUT和10。"q"列表获取输入框中的选中项,字符串"s"数组从q列表中获取拆分后的数据。然后"numbers"列表只得到字符串的数字部分,例如"INPUT 5",数字列表将只得到"5"。然后我想对这些数字进行排序,并构建另一个字符串,将排序后的数字列表和字符串"INPUT"组合在一起,并将其添加到输出checklistbox中。我的代码不工作…有什么建议吗?它应该对数字进行排序,但它没有……谁有任何建议,为什么这个代码不能工作?我一直得到数组的错误消息,能够取消处理负整数和什么不。

    List<string> q = new List<string>();
    List<string> numbers = new List<string>();
    private void button_ekle_Click(object sender, EventArgs e)
    {


        for (int k = clb_input.Items.Count - 1; k >= 0; k--)
        {
            if (clb_input.GetItemChecked(k) == true)
            {
                q.Add(clb_input.Items[k].ToString());

                //clb_output.Items.Add(clb_input.Items[k]);
                clb_input.Items.RemoveAt(k);
            }
            else { }
        }
        string[] s = new string[q.Count * 2];
        //string[] numbers=new string[q.Count/2];
        for (int t = 1; t <= q.Count * 2; t++)
        {
            if (q != null)
                s = q[t - 1].ToString().Split(' ');
            else { s[t] = null; }
        }
        for (int x = 1; x <= q.Count; x++)
        {
            if (s[2 * x - 1] != null)
            {
                numbers[x - 1] = s[2 * x - 1];
                numbers.Sort();
                clb_output.Items.Add("INPUT "+ numbers[x - 1].ToString());
            }
            else { numbers[x - 1] = null; }
        } 

    }

按字母顺序和数字顺序排序列表

您需要的是Alphanumeric Sorting(最常见于windows资源管理器,文件排序方式)

代码可以在这里找到:http://www.dotnetperls.com/alphanumeric-sorting

示例

class Program
{
    static void Main()
    {
    string[] highways = new string[]
    {
        "100F",
        "50F",
        "SR100",
        "SR9"
    };
    //
    // We want to sort a string array called highways in an
    // alphanumeric way. Call the static Array.Sort method.
    //
    Array.Sort(highways, new AlphanumComparatorFast());
    //
    // Display the results
    //
    foreach (string h in highways)
    {
        Console.WriteLine(h);
    }
    }
}

输出
50F
100F
SR9
SR100
实施

public class AlphanumComparatorFast : IComparer
{
    public int Compare(object x, object y)
    {
    string s1 = x as string;
    if (s1 == null)
    {
        return 0;
    }
    string s2 = y as string;
    if (s2 == null)
    {
        return 0;
    }
    int len1 = s1.Length;
    int len2 = s2.Length;
    int marker1 = 0;
    int marker2 = 0;
    // Walk through two the strings with two markers.
    while (marker1 < len1 && marker2 < len2)
    {
        char ch1 = s1[marker1];
        char ch2 = s2[marker2];
        // Some buffers we can build up characters in for each chunk.
        char[] space1 = new char[len1];
        int loc1 = 0;
        char[] space2 = new char[len2];
        int loc2 = 0;
        // Walk through all following characters that are digits or
        // characters in BOTH strings starting at the appropriate marker.
        // Collect char arrays.
        do
        {
        space1[loc1++] = ch1;
        marker1++;
        if (marker1 < len1)
        {
            ch1 = s1[marker1];
        }
        else
        {
            break;
        }
        } while (char.IsDigit(ch1) == char.IsDigit(space1[0]));
        do
        {
        space2[loc2++] = ch2;
        marker2++;
        if (marker2 < len2)
        {
            ch2 = s2[marker2];
        }
        else
        {
            break;
        }
        } while (char.IsDigit(ch2) == char.IsDigit(space2[0]));
        // If we have collected numbers, compare them numerically.
        // Otherwise, if we have strings, compare them alphabetically.
        string str1 = new string(space1);
        string str2 = new string(space2);
        int result;
        if (char.IsDigit(space1[0]) && char.IsDigit(space2[0]))
        {
        int thisNumericChunk = int.Parse(str1);
        int thatNumericChunk = int.Parse(str2);
        result = thisNumericChunk.CompareTo(thatNumericChunk);
        }
        else
        {
        result = str1.CompareTo(str2);
        }
        if (result != 0)
        {
        return result;
        }
    }
    return len1 - len2;
    }
}

最简单的解决方法就是在数值之间左填充一个相同长度的空格。

List<string> lst = new List<string>
{
    "Item   9",
    "Item  999",
    "Thing 999",
    "Thing   5",
    "Thing   1",
    "Item  20",
    "Item  10",
};
lst.Sort();
输出:

Item   9 
Item  10 
Item  20 
Item  999 
Thing   1 
Thing   5 
Thing 999 

并且您总是可以在排序操作执行后删除用于填充的额外空白

您可以像这样对比较器使用Sort:

List<string> q = new List<string>();
private void button_ekle_Click(object sender, EventArgs e)
{
    for (int k=clb_input.Items.Count-1; k >= 0; k--)
    {
        if (clb_input.GetItemChecked(k) == true)
        {
            q.Add(clb_input.Items[k].ToString());
            clb_input.Items.RemoveAt(k);
        }
        else { }
    }
    q.Sort((p1,p2)=>((int)(p1.split(" ")[1])).CompareTo((int)(p2.split(" ")[1])));
    for (int t = 0; t < q.Count; t++)
    {
        clb_output.Items.Add(q[t].ToString());
    }
}