按 1 键数组对 8 个数组进行排序,加上问题姓氏名字,排序
本文关键字:数组 排序 问题 | 更新日期: 2023-09-27 18:32:58
下面的代码。想法是对姓氏数组进行排序,并使所有数据相对于姓氏移动,因此这里是示例数据:
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];
}
}
你不需要做你正在做的事情。mClients
数组中已经有一个Client
对象。因此,只需直接对其进行排序,而不是将其拆分为 7 个数组并对数组进行排序。
List<Client> sortedClients = mClients.OrderBy(a => a.LastName)
.ThenBy(b => b.FirstName)
.ToList();