Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Array methods (Sort, Reverse, IndexOf) in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Array methods (Sort, Reverse, IndexOf)
Start with array
↓
Choose method: Sort, Reverse, or IndexOf
↓
Sort: rearrange elements ascending
↓
Reverse: flip elements order
↓
IndexOf: find position of element
↓
Return result or modify array
↓
End
Start with an array, pick a method (Sort, Reverse, IndexOf), then either rearrange elements or find an element's position, and finally get the result.
Execution Sample
C Sharp (C#)
int[] numbers = {3, 1, 4, 1, 5};
Array.Sort(numbers);
Array.Reverse(numbers);
int pos = Array.IndexOf(numbers, 1);
Sorts the array ascending, reverses it, then finds the index of the first '1'.
Execution Table
StepActionArray StateIndexOf Search ElementIndexOf Result
1Initial array[3, 1, 4, 1, 5]--
2Sort array ascending[1, 1, 3, 4, 5]--
3Reverse array[5, 4, 3, 1, 1]--
4IndexOf search for 1[5, 4, 3, 1, 1]13
5End[5, 4, 3, 1, 1]--
💡 IndexOf found element '1' at position 3, execution ends.
Variable Tracker
VariableStartAfter SortAfter ReverseAfter IndexOf
numbers[3,1,4,1,5][1,1,3,4,5][5,4,3,1,1][5,4,3,1,1]
posundefinedundefinedundefined3
Key Moments - 3 Insights
Why does IndexOf return 3 instead of 1 when searching for '1'?
Because after sorting and reversing, the first '1' is at index 3 in the modified array (see execution_table step 4).
Does Array.Sort change the original array or create a new one?
Array.Sort changes the original array in place, as shown by the array state changing from step 1 to step 2.
What happens if the element searched by IndexOf is not in the array?
IndexOf returns -1 to indicate the element is not found, but in this example the element '1' is found at index 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the array state after the Reverse method?
A[3, 1, 4, 1, 5]
B[5, 4, 3, 1, 1]
C[1, 1, 3, 4, 5]
D[1, 3, 4, 5, 1]
💡 Hint
Check the 'Array State' column at step 3 in the execution_table.
At which step does the array get sorted in ascending order?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column and array changes between step 1 and 2.
If we searched for '2' using IndexOf instead of '1', what would be the result?
A-1
B3
C0
D4
💡 Hint
Recall that IndexOf returns -1 if the element is not found in the array.
Concept Snapshot
Array methods in C#:
- Array.Sort(array): sorts elements ascending in place.
- Array.Reverse(array): reverses elements order in place.
- Array.IndexOf(array, value): returns first index of value or -1 if not found.
These methods modify the original array except IndexOf which returns a position.
Full Transcript
We start with an array of numbers. First, Array.Sort rearranges the elements in ascending order, changing the array from [3,1,4,1,5] to [1,1,3,4,5]. Next, Array.Reverse flips the array order to [5,4,3,1,1]. Then, Array.IndexOf searches for the first occurrence of '1' in the reversed array and finds it at index 3. The variable 'pos' stores this index. The array methods Sort and Reverse change the original array directly, while IndexOf returns the position of the searched element or -1 if not found.

Practice

(1/5)
1. What does the Array.Sort method do to an array in C#?
easy
A. It reverses the order of the items.
B. It arranges the items in ascending order.
C. It finds the index of a specific item.
D. It removes duplicate items from the array.

Solution

  1. Step 1: Understand Array.Sort purpose

    The Array.Sort method organizes the elements of an array in ascending order by default.
  2. Step 2: Compare with other methods

    Array.Reverse flips the array, and Array.IndexOf finds an element's position, so they do different things.
  3. Final Answer:

    It arranges the items in ascending order. -> Option B
  4. Quick Check:

    Array.Sort = ascending order [OK]
Hint: Sort means arrange from smallest to largest [OK]
Common Mistakes:
  • Confusing Sort with Reverse
  • Thinking Sort finds an element's position
  • Assuming Sort removes duplicates
2. Which of the following is the correct way to reverse an array named numbers in C#?
easy
A. numbers.Reverse();
B. numbers.ReverseArray();
C. Array.Sort(numbers);
D. Array.Reverse(numbers);

Solution

  1. Step 1: Identify correct method call syntax

    In C#, Array.Reverse is a static method that takes the array as an argument, so Array.Reverse(numbers); is correct.
  2. Step 2: Check other options

    numbers.Reverse() is a LINQ extension returning IEnumerable, not reversing in place. Array.Sort sorts, not reverses. ReverseArray() does not exist.
  3. Final Answer:

    Array.Reverse(numbers); -> Option D
  4. Quick Check:

    Reverse array = Array.Reverse(array) [OK]
Hint: Use Array.Reverse(array) to flip order [OK]
Common Mistakes:
  • Using instance method Reverse() which doesn't reverse in place
  • Confusing Sort with Reverse
  • Calling non-existent methods
3. What will be the output of this C# code?
string[] fruits = {"apple", "banana", "cherry"};
Array.Sort(fruits);
Array.Reverse(fruits);
Console.WriteLine(fruits[0]);
medium
A. cherry
B. banana
C. apple
D. Runtime error

Solution

  1. Step 1: Sort the array

    After Array.Sort(fruits);, the array becomes ["apple", "banana", "cherry"] (already sorted).
  2. Step 2: Reverse the array

    After Array.Reverse(fruits);, the array becomes ["cherry", "banana", "apple"].
  3. Step 3: Print first element

    fruits[0] is now "cherry".
  4. Final Answer:

    cherry -> Option A
  5. Quick Check:

    Sort then Reverse = descending order [OK]
Hint: Sort then Reverse = descending order, first item is largest [OK]
Common Mistakes:
  • Assuming array stays sorted ascending
  • Confusing Reverse with Sort
  • Expecting runtime error due to method calls
4. The following code throws an error. What is the mistake?
int[] nums = {3, 1, 4};
nums.Sort();
Console.WriteLine(nums[0]);
medium
A. Arrays don't have a Sort() instance method.
B. The array must be reversed before sorting.
C. You must use Console.WriteLine(nums.Sort()); instead.
D. The array must be converted to a list before sorting.

Solution

  1. Step 1: Identify method call error

    Arrays in C# do not have an instance method called Sort(). Sorting is done via the static method Array.Sort(array).
  2. Step 2: Understand correct usage

    The correct way is Array.Sort(nums); to sort the array in place.
  3. Final Answer:

    Arrays don't have a Sort() instance method. -> Option A
  4. Quick Check:

    Use Array.Sort(array), not array.Sort() [OK]
Hint: Array.Sort is static; no instance Sort() method [OK]
Common Mistakes:
  • Calling Sort() directly on array instance
  • Confusing List.Sort() with Array.Sort()
  • Trying to print Sort() return value
5. Given the array int[] values = {5, 3, 8, 3, 9};, which code snippet finds the index of the first occurrence of the number 3 and then reverses the array?
hard
A. int index = Array.FindIndex(values, 3); Array.Sort(values);
B. int index = values.IndexOf(3); Array.Reverse(values);
C. int index = Array.IndexOf(values, 3); Array.Reverse(values);
D. int index = Array.IndexOf(values, 3); Array.Sort(values);

Solution

  1. Step 1: Find index of first 3

    Array.IndexOf(values, 3) correctly returns the first index where 3 appears, which is 1.
  2. Step 2: Reverse the array

    Array.Reverse(values); reverses the array in place as required.
  3. Final Answer:

    int index = Array.IndexOf(values, 3); Array.Reverse(values); -> Option C
  4. Quick Check:

    IndexOf + Reverse = int index = Array.IndexOf(values, 3); Array.Reverse(values); [OK]
Hint: Use Array.IndexOf(array, value) to find index [OK]
Common Mistakes:
  • Using instance method IndexOf on array (not valid)
  • Confusing FindIndex with IndexOf
  • Sorting instead of reversing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard