代码如下.想法是对Last name数组进行排序,并使所有数据相对于姓氏移动,所以这里是示例数据:
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
Run Code Online (Sandbox Code Playgroud)
这是它应该看起来像排序:
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
Run Code Online (Sandbox Code Playgroud)
这是我得到的
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
Run Code Online (Sandbox Code Playgroud)
作为一个额外的问题,当我再次排序时,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];
}
}
Run Code Online (Sandbox Code Playgroud)
你不应该做你正在做的事情.数组中已有一个Client对象 mClients.所以直接对它进行排序,而不是将其拆分为7个数组并对数组进行排序.
List<Client> sortedClients = mClients.OrderBy(a => a.LastName)
.ThenBy(b => b.FirstName)
.ToList();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
120 次 |
| 最近记录: |