簡體   English   中英

按字母順序和數字順序對列表進行排序

[英]Sorting a List alphabetically and numerically

我編寫了一個具有列表的代碼,我想按字母順序和數字順序對列表進行排序。

例如,列表中的第一項是

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

我希望我的清單像這樣重組:

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

因此,基本上,我的程序從選中的列表框中獲取選中的項目,將其存儲在列表中,並且我希望它按字母順序重新組織列表。

選中的列表框包含諸如輸入1,輸入2,輸入3 ...等第四項。 誰能建議我解決該問題的方法?

更新的代碼

我已經更新了代碼,現在此代碼將字符串分為INPUT和10。“ q”列表獲取輸入框中的已選中項,字符串“ s”數組從q列表獲取splittd數據。 然后“數字”列表僅獲取字符串的數字部分,例如“ 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; }
        } 



    }

您需要的是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;
    }
}

您可以將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());
    }
}

最簡單的解決方案是將數值左鍵填入相同長度的空格。

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 

並且,在執行排序操作之后,您始終可以刪除用於填充的多余空白。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM