I need to sort an array of strings based on the array of ints.
I have this to sort my int array
for (int pass = 0; pass < score.Length - ONE; pass++)
{
for (int index = 0; index < score.Length - ONE; index++)
{
if (score[index] > score[index + ONE])
{
int temp = score[index];
score[index] = score[index + ONE];
score[index + 1] = temp;
}
}
}
This int and name array were obtained by
Console.Write("Enter in a name and score: ", i);
userInput = Console.ReadLine();
if (userInput != "")
{
parsedInput = userInput.Split();
name[i] = parsedInput[ZERO];
score[i] = int.Parse(parsedInput[ONE]);
}
For my assignment I need to display the name and their scores organized by the highest score.
I know I could use Array.Sort(score, name) to achieve this but the assignment wants me to not use any of the built in sorting algorithms in the .net library, which is assume Array.Sort would be.
Any tips or help would be greatly appreciated.
0and1are not magic numbers in this context. An example of a magic number would be something like2781666meaning "sort by name, then by date, and return hidden records", which indeed is better abstracted by constants or enumerated flags.ONEandZEROare still magic numbers, only disguised as constants. To really avoid magic numbers you should use a constant for each use of a number, for exampleconst int ItemDistance = 1;to use in the loop to get to the previous and next item, andconst int NameIndex = 0; const int ScoreIndex = 1;to access the items in the array.