【问题标题】:Sorting a List alphabetically and numerically按字母和数字排序列表
【发布时间】:2014-10-08 04:31:09
【问题描述】:

我编写了一个包含列表的代码,我想按字母和数字对列表进行排序。

例如列表中的第一项是

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

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

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

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

选中的列表框有 INPUT 1、INPUT 2、INPUT 3...等第四项。谁能建议我如何解决这个问题?

更新代码

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

    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; }
        } 



    }

【问题讨论】:

  • 错字“我想像 list[0]=INPUT 5 和 list[1]=10 一样重新组织我的列表”?
  • 如何更改列表框以使用零填充数字,例如INPUT 010,所以字母排序也是数字的?
  • 您可以剥离“INPUT”部分,并将其余部分解析为 int。当您再次添加它们时,您可以再次构建字符串:“INPUT” + intval。
  • 我最初的想法是拆分字符串并比较数字,但后来索引混淆了,我无法完全编写代码,所以我想看看是否有类似的方法来列出。排序()
  • 我已经更新了我的代码,所以它现在拆分了字符串,我尝试对数字进行数字排序,但我不断收到我的数组无法获得负整数的错误

标签: c# arrays list sorting dynamic


【解决方案1】:

您可以像这样将 Sort 与 Comparer 一起使用:

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

【讨论】:

  • 在这种情况下 p1 和 p2 是什么?
  • p1 和 p2 是比较器函数中的参数,采用 lambda 表示法。
【解决方案2】:

你需要的是Alphanumeric Sorting(最常见于windows explorer,文件排序方式)

代码可以在这里找到: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;
    }
}

【讨论】:

  • @CarstenKönig - 只是这样做 :) !
【解决方案3】:

最简单的解决方案是用空格将数值左填充到相同的长度。

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 

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

【讨论】:

  • 有一个 padleft 方法我试过了,但也没有用
  • 我在新表单上运行了您的代码,您的代码正在运行...您能否详细说明如何离开 pad...我不太明白您的意思
  • 好吧,没关系,我添加了选中列表框的项目,以便这些位置与选中列表框项目上的每个数字对齐。所以对于数字“1”,我写了“1”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-19
  • 2016-03-07
  • 1970-01-01
  • 2020-09-03
  • 1970-01-01
  • 2018-09-15
相关资源
最近更新 更多