List <string []>排序不起作用</string []>

时间:2012-05-08 20:48:01

标签: c# .net string list sorting

我想对List进行排序,其中字符串数组的最后一个元素是sort键。我需要前5名的成绩。(用较低的键)

这有效,但我不想使用LINQ:

    ...
    List<string[]> gameResults = OpenResults(fileLocation);
    gameResults.Add(CurrentPlayerResult());

    var newOrderedGameResults =
    from line in currentGameResults
    orderby int.Parse(line.LastOrDefault())
    select line;
    ...

但这不是:

    public void SaveResults(string fileLocation = @"..\..\GameResults.txt")
    {
        // string[] format:
        // [0],..,[n-1], [n]
        // names,        mistakeCount 
        List<string[]> gameResults = OpenResults(fileLocation);
        gameResults.Add(CurrentPlayerResult()); 

        QuickSort(gameResults, 0, gameResults.Count - 1);
        try
        {
            using (StreamWriter resultsFile = new StreamWriter(fileLocation))
            {
                foreach (var line in gameResults.Take(5))
                {
                    for (int i = 0; i < line.Length - 1; i++)
                    {
                        resultsFile.Write("{0} ", line[i]);
                    }
                    // dont add " " after last element
                    resultsFile.WriteLine("{0}", line[line.Length - 1]);
                }
            }
        }
        catch (IOException exception)
        {
            Console.WriteLine("The file could not be write:");
            Console.WriteLine(exception.Message);
        }

其中:

    private void QuickSort(List<string[]> listToSort, int left, int right)
    {
        int pivot = left; //(left + right) / 2;
        int leftHold = left;
        int rightHold = right;

        while (left < right)
        {
            while (GetScoreFromLine(listToSort[right]) >= pivot && left < right)
            {
                right--;
            }
            if (left != right)
            {
                listToSort[left] = listToSort[right];
                left++;
            }

            while (GetScoreFromLine(listToSort[left]) <= pivot && left < right)
            {
                left++;
            }
            if (left != right)
            {
                listToSort[right] = listToSort[left];
                right--;
            }
        }
        listToSort[left] = listToSort[pivot];
        pivot = left;
        left = leftHold;
        right = rightHold;

        if (left < pivot)
        {
            QuickSort(listToSort, left, pivot - 1);
        }
        if (right > pivot)
        {
            QuickSort(listToSort, pivot + 1, right);
        }
    }

    private int GetScoreFromLine(string[] lineToParce)
    {
        int length = lineToParce.Length;
        return int.Parse(lineToParce[length - 1]);
    }

不要正常工作。

有没有办法使用ARRAY.SORT? 谁能帮忙。 感谢。

2 个答案:

答案 0 :(得分:3)

你使用List,所以你也可以使用List.Sort。使用比较器代表或者如果您还坚持使用比较器类。

List<string[]> lists = new List<string[]>
{
     new string[] { "1", "b", "5"},
     new string[] { "2", "b", "3"},
     new string[] { "3", "b", "1"},
     new string[] { "4", "b", "2"},
};
lists.Sort((a, b) => int.Parse(a[a.Length - 1]) - int.Parse(b[b.Length - 1]));

我不确定为什么在显示带List的示例时特别要求Array.Sort。无论如何,如果需要,可以使用List.ToArray()然后使用

var arr = lists.ToArray();
Array.Sort(arr, (a, b) => int.Parse(a[a.Length - 1]) - int.Parse(b[b.Length - 1]));

这个版本几乎相同,并且很好地展示了.NET Framework在2005年使用.NET 2.0添加到CLR的多年来一直发展的一致性。

答案 1 :(得分:0)

以下内容可以满足您的需求:

public void SaveResults(string fileLocation = @"..\..\GameResults.txt")
{
    // string[] format:
    // [0],..,[n-1], [n]
    // names,        mistakeCount 
    List<string[]> gameResults = OpenResults(fileLocation);
    gameResults.Add(CurrentPlayerResult()); 
    gameResults.Sort(CompareResults);
    ...
}

private int CompareResults(string[] left, string[] right)
{
    if ((left == null && right == null) || (left.Length == 0 && right.Length == 0))
        return 0;
    else if (left == null || left.Length == 0)
        return 1;
    else if (right == null || right.Length == 0)
        return -1;

    int leftVal = int.Parse(left[left.Length - 1]);
    int rightVal = int.Parse(right[right.Length - 1]);

    return leftVal.CompareTo(rightVal);
}