0

请参阅下面的列表:

    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_countline_acc_ar[4]部分排序!
那部分是一个字符串,如 -> 325423 -> 意味着我可以将它转换为整数。
我怎么能做这份工作?

4

5 回答 5

1

One way is to implement the comparer and provide it as an argument to the Sort function:

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

And to sort call it like this:

list_lines_silver_count.Sort(new SilverCountLineComparer());
于 2012-10-30T11:20:09.293 回答
1

Change the foreach:

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

}

Further refactorings could make the code more elegant, but I think this piece of LINQ should do the job.

于 2012-10-30T11:21:12.337 回答
1

You can try this

sorted using a Comparison generic delegate representing the CompareStringByInteger method

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

and apply it in sort method.

list_lines_silver_count.Sort(CompareStringByInteger);
于 2012-10-30T11:21:13.397 回答
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();
于 2012-10-30T11:19:45.270 回答
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());
于 2012-10-30T11:21:36.207 回答