Reputation: 348
Code below. Idea is to sort the Last name array and have all the data move in relation to the last name, so here is the sample data:
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
And here is what it should look like sorted:
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
Here is what I am getting
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
As an additional issue when I sort again, Amy and Scott Wilson switch places, all the rest of the data stays sorted.
What am I doing wrong?
Code:
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];
}
}
Upvotes: 0
Views: 126
Reputation: 44288
You shouldn't need to do what you're doing. You have a Client
object already in the mClients
array. So just sort that directly, instead of splitting it into 7 arrays and sorting the arrays.
List<Client> sortedClients = mClients.OrderBy(a => a.LastName)
.ThenBy(b => b.FirstName)
.ToList();
Upvotes: 5