0

代码如下。想法是对姓氏数组进行排序,并让所有数据相对于姓氏移动,所以这里是示例数据:

         Amy    Wilson  21  68.5    190 150 10
         Scott  Wilson  25  76.5    250 210 10
         Jamie  Scott   45  62  150 135 56
         Sharon Baxter  52  65  150 140 8
         Brock  Stanley 65  70  180 190 4
         Baxter Cash    18  72  170 200 8
         John   Stanford    30  74  190 210 7
         Angel  Delgado 25  62.5    150 137 5
         Brad   Harris  55  70  200 180 6
         Amber  Carrell 18  65  120 110 3
         Jakob  Neihaus 20  64  110 120 3
         Willie Mitchell    23  68  150 170 6
         Melia  Mugano  18  67  167 145 7

这是排序后的样子:

        Sharon  Baxter  52  65  150 140 8
        Amber   Carrell 18  65  120 110 3
        Baxter  Cash    18  72  170 200 8
        Angel   Delgado 25  62.5    150 137 5
        Brad    Harris  55  70  200 180 6
        Willie  Mitchell    23  68  150 170 6
        Melia   Mugano  18  67  167 145 7
        Jakob   Neihaus 20  64  110 120 3
        Jamie   Scott   45  62  150 135 56
        John    Stanford    30  74  190 210 7
        Brock   Stanley 65  70  180 190 4
        Amy Wilson  21  68.5    190 150 10
        Scott   Wilson  25  76.5    250 210 10

这是我得到的

        Sharon  Baxter  52  68.5    190 150 10
        Amber   Carrell 18  76.5    250 210 10
        Baxter  Cash    18  62  150 135 56
        Angel   Delgado 25  65  150 140 8
        Brad    Harris  55  70  180 190 4
        Willie  Mitchell    23  72  170 200 8
        Melia   Mugano  18  74  190 210 7
        Jakob   Neihaus 20  62.5    150 137 5
        Jamie   Scott   45  70  200 180 6
        John    Stanford    30  65  120 110 3
        Brock   Stanley 65  64  110 120 3
        Amy Wilson  21  67  167 145 7
        Scott   Wilson  25  68  150 170 6

作为我再次排序时的另一个问题,Amy 和 Scott Wilson 交换位置,所有其余数据都保持排序状态。

我究竟做错了什么?

代码:

private void sortArray()
{

    string[]  clientFirstnameArray = new string[mMaxClients];
    string[]  clientLastnameArray = new string[mMaxClients];
    int[]  clientAgeArray = new int[mMaxClients];
    double[] clientHeightArray = new double[mMaxClients];
    double[] clientStartWeightArray = new double[mMaxClients];
    double[] goalWeightArray = new double[mMaxClients];
    int[] weeksArray = new int[mMaxClients];

    for (int index = 0; index < mNumClient; index++)
    {
        clientLastnameArray[index] = mClients[index].LastName;
        clientFirstnameArray[index] = mClients[index].FirstName;
        clientAgeArray[index] = mClients[index].Age;
        clientHeightArray[index] = mClients[index].Height;
        clientStartWeightArray[index] = mClients[index].StartWeight;
        goalWeightArray[index] = mClients[index].GoalWeight;
        weeksArray[index] = mClients[index].Weeks;

    }
    string[] copy_clientLastnameArray = new string[mMaxClients];

    Array.Copy(clientLastnameArray, 0, copy_clientLastnameArray, 0, mNumClient);
    Array.Sort(clientLastnameArray, clientFirstnameArray, 0, mNumClient);
    Array.Sort(copy_clientLastnameArray, clientAgeArray, 0, mNumClient);
    Array.Sort(copy_clientLastnameArray, clientHeightArray, 0, mNumClient);
    Array.Sort(copy_clientLastnameArray, clientStartWeightArray, 0, mNumClient);
    Array.Sort(copy_clientLastnameArray, goalWeightArray, 0, mNumClient);
    Array.Sort(copy_clientLastnameArray, weeksArray, 0, mNumClient);


    for (int index = 0; index < mNumClient; index++)
    {
        mClients[index].LastName =  clientLastnameArray[index];
        mClients[index].FirstName =  clientFirstnameArray[index];
        mClients[index].Age =  clientAgeArray[index];
        mClients[index].Height = clientHeightArray[index];
        mClients[index].StartWeight = clientStartWeightArray[index];
        mClients[index].GoalWeight = goalWeightArray[index];
        mClients[index].Weeks = weeksArray[index];
    }
}
4

1 回答 1

5

你不应该做你正在做的事情。数组中已经有一个Client对象 mClients。所以直接对它进行排序,而不是将其拆分为 7 个数组并对数组进行排序。

 List<Client> sortedClients = mClients.OrderBy(a => a.LastName)
                                      .ThenBy(b => b.FirstName)
                                      .ToList();
于 2013-09-27T10:34:55.633 回答