如何使用C#为列表中的每个项目的特定部分排序列表

时间:2012-10-30 11:12:09

标签: c# list sorting

请参阅以下列表:

    string[] Separator = new string[] { "__" };

    string[] lines_acc = File.ReadAllLines(accfilePath);
    List<string> list_lines_acc = new List<string>(lines_acc);

    List<string> list_lines_silver_count = new List<string>();
    FileStream fs_ = null;
    if (!File.Exists(silver_countfilePath))
    {
        using (fs_ = File.Create(silver_countfilePath))
        {
            foreach (string line_acc in list_lines_acc)
            {
                string[] line_acc_ar = line_acc.Split(Separator, StringSplitOptions.None);
                string line_acc_new = line_acc_ar[0] + "__" + line_acc_ar[1] + "__" + line_acc_ar[3] + "__" + line_acc_ar[4] + "__" + "0";
                list_lines_silver_count.Add(line_acc_new);
            }

            File.WriteAllLines(silver_countfilePath, list_lines_silver_count);
        }
    }
    else
    {
        string[] lines_silver_count = File.ReadAllLines(silver_countfilePath);
        list_lines_silver_count = new List<string>(lines_silver_count);
    }

我想按list_lines_silver_count部分排序line_acc_ar[4]! 那部分是一个字符串,如 - &gt; 325423 - &gt;意思是我可以将它转换为整数 我怎么能做那份工作?

5 个答案:

答案 0 :(得分:1)

您可以使用Linq:

int number = 0;
string[] lines_silver_count = File
                .ReadLines(silver_countfilePath)
                .Select(l => new { 
                    Line = l, 
                    Parts = l.Split(Separator, StringSplitOptions.None) 
                })
                .Where(x => x.Parts.Length > 4
                    && int.TryParse(x.Parts[4], out number))
                .OrderBy(x => number)
                .Select(x => x.Line)
                .ToArray();

答案 1 :(得分:1)

一种方法是实现比较器并将其作为Sort函数的参数提供:

public class SilverCountLineComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {
        string xPart = x.Split(new char[] {'_'}, StringSplitOptions.RemoveEmptyEntries)[3];
        string yPart = y.Split(new char[] {'_'}, StringSplitOptions.RemoveEmptyEntries)[3];

        int xNum = Int32.Parse(xPart);
        int yNum = Int32.Parse(yPart);

        return xNum.CompareTo(yNum);
    }
}

并按照这样排序:

list_lines_silver_count.Sort(new SilverCountLineComparer());

答案 2 :(得分:1)

更改foreach

foreach (var line_acc_ar in list_lines_acc
    .Select(s => s.Split(Separator, StringSplitOptions.None)
    .OrderBy(a => a[4])) {

}

进一步的重构可以使代码更优雅,但我认为LINQ应该能够完成这项工作。

答案 3 :(得分:1)

你可以试试这个

使用表示CompareStringByInteger方法的比较通用委托进行排序

public static int CompareStringByInteger(string x, string y)
{
    if (x == null)
    {
        if (y == null)
            return 0;
        else
                return -1;
    }
    else
    {
        try{
            return Convert.ToInt32(x).CompareTo(Convert.ToInt32(y));
        }catch{
            return x.CompareTo(y);
        }
    }
}

并将其应用于排序方法。

list_lines_silver_count.Sort(CompareStringByInteger);

答案 4 :(得分:0)

public class MyCompare : IComparer<string> 
{ 
    public int Compare(string x, string y) 
    { 
        //get line_acc_ar[4] part from strings x and y
        string[] Separator = new string[] { "__" };
        string partX = x.Split(Separator, StringSplitOptions.None)[3];
        string partY = y.Split(Separator, StringSplitOptions.None)[3];


        int intPartX = int.Parse(partX );
        int inrPartY = int.Parse(partY );
        return intPartX.CompareTo(inrPartY)

    } 
}
list_lines_silver_count.OrderBy(a => a, new MyCompare());