0

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.

6
  • Yes, ZERO and ONE are constants Commented Nov 29, 2013 at 23:39
  • may be args value passed to the main right ? Commented Nov 29, 2013 at 23:42
  • 1
    @user2781666, 0 and 1 are not magic numbers in this context. An example of a magic number would be something like 2781666 meaning "sort by name, then by date, and return hidden records", which indeed is better abstracted by constants or enumerated flags. Commented Nov 29, 2013 at 23:45
  • 2
    @user2781666: The constants ONE and ZERO are still magic numbers, only disguised as constants. To really avoid magic numbers you should use a constant for each use of a number, for example const int ItemDistance = 1; to use in the loop to get to the previous and next item, and const int NameIndex = 0; const int ScoreIndex = 1; to access the items in the array. Commented Nov 29, 2013 at 23:50
  • 1
    Oh that makes sense, so when it's a basic algorithm form then 1 and 0 would be ok? That makes a lot more sense Commented Nov 29, 2013 at 23:50

2 Answers 2

1

You need to rearrange name when you are sorting score so that they are consistent.

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;

            string temp2 = name[index];
            name[index] = name[index + ONE];
            name[index + 1] = temp2;
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome, that works perfectly. I didn't think it would be that easy.
If this answer helped you, you can accept the answer with the check mark to indicate that you no longer require help with this question.
0

When you swap the items in the int array, also swap the corresponding items in the string array. That way the values follow each other, and the name and score remain in sync.

Note that the sorting algorithm is an inefficient version of bubble sort. If there are no swaps in a run of the inner loop, the array is sorted and you can exit out of the outer loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.