Array methods help you organize and find items in a list easily. Sorting arranges items, reversing flips their order, and IndexOf finds where an item is.
Array methods (Sort, Reverse, IndexOf) in C Sharp (C#)
using System; class Program { static void Main() { int[] numbers = { 5, 3, 8, 1 }; // Sort the array Array.Sort(numbers); // Reverse the array Array.Reverse(numbers); // Find index of an element int position = Array.IndexOf(numbers, 3); } }
Array.Sort(array) changes the array to be in ascending order.
Array.Reverse(array) flips the order of elements in the array.
int[] emptyArray = {}; Array.Sort(emptyArray); Array.Reverse(emptyArray); int index = Array.IndexOf(emptyArray, 10);
int[] singleElement = { 42 }; Array.Sort(singleElement); Array.Reverse(singleElement); int index = Array.IndexOf(singleElement, 42);
int[] numbers = { 10, 20, 30, 40 }; Array.Sort(numbers); // Already sorted Array.Reverse(numbers); // Now reversed int indexStart = Array.IndexOf(numbers, 40); // First element after reverse int indexEnd = Array.IndexOf(numbers, 10); // Last element after reverse
This program shows how to sort, reverse, and find the index of elements in an array. It prints the array before and after changes and shows what happens when searching for a missing item.
using System; class Program { static void Main() { int[] numbers = { 7, 2, 9, 4, 3 }; Console.WriteLine("Original array:"); PrintArray(numbers); Array.Sort(numbers); Console.WriteLine("\nAfter sorting:"); PrintArray(numbers); Array.Reverse(numbers); Console.WriteLine("\nAfter reversing:"); PrintArray(numbers); int searchValue = 4; int index = Array.IndexOf(numbers, searchValue); Console.WriteLine($"\nIndex of {searchValue}: {index}"); int missingValue = 10; int missingIndex = Array.IndexOf(numbers, missingValue); Console.WriteLine($"Index of {missingValue} (not in array): {missingIndex}"); } static void PrintArray(int[] array) { foreach (int number in array) { Console.Write(number + " "); } Console.WriteLine(); } }
Time complexity: Sort is O(n log n), Reverse is O(n), IndexOf is O(n).
Space complexity: These methods work in place, so they use little extra memory.
Common mistake: Forgetting that IndexOf returns -1 if the item is not found.
Use Sort when you need ordered data, Reverse when you want to flip order, and IndexOf to find positions.
Array.Sort arranges items in ascending order.
Array.Reverse flips the order of items in the array.
Array.IndexOf finds the position of an item or returns -1 if not found.
Practice
Array.Sort method do to an array in C#?Solution
Step 1: Understand Array.Sort purpose
TheArray.Sortmethod organizes the elements of an array in ascending order by default.Step 2: Compare with other methods
Array.Reverseflips the array, andArray.IndexOffinds an element's position, so they do different things.Final Answer:
It arranges the items in ascending order. -> Option BQuick Check:
Array.Sort = ascending order [OK]
- Confusing Sort with Reverse
- Thinking Sort finds an element's position
- Assuming Sort removes duplicates
numbers in C#?Solution
Step 1: Identify correct method call syntax
In C#,Array.Reverseis a static method that takes the array as an argument, soArray.Reverse(numbers);is correct.Step 2: Check other options
numbers.Reverse()is a LINQ extension returning IEnumerable, not reversing in place.Array.Sortsorts, not reverses.ReverseArray()does not exist.Final Answer:
Array.Reverse(numbers); -> Option DQuick Check:
Reverse array = Array.Reverse(array) [OK]
- Using instance method Reverse() which doesn't reverse in place
- Confusing Sort with Reverse
- Calling non-existent methods
string[] fruits = {"apple", "banana", "cherry"};
Array.Sort(fruits);
Array.Reverse(fruits);
Console.WriteLine(fruits[0]);Solution
Step 1: Sort the array
AfterArray.Sort(fruits);, the array becomes ["apple", "banana", "cherry"] (already sorted).Step 2: Reverse the array
AfterArray.Reverse(fruits);, the array becomes ["cherry", "banana", "apple"].Step 3: Print first element
fruits[0]is now "cherry".Final Answer:
cherry -> Option AQuick Check:
Sort then Reverse = descending order [OK]
- Assuming array stays sorted ascending
- Confusing Reverse with Sort
- Expecting runtime error due to method calls
int[] nums = {3, 1, 4};
nums.Sort();
Console.WriteLine(nums[0]);Solution
Step 1: Identify method call error
Arrays in C# do not have an instance method calledSort(). Sorting is done via the static methodArray.Sort(array).Step 2: Understand correct usage
The correct way isArray.Sort(nums);to sort the array in place.Final Answer:
Arrays don't have a Sort() instance method. -> Option AQuick Check:
Use Array.Sort(array), not array.Sort() [OK]
- Calling Sort() directly on array instance
- Confusing List.Sort() with Array.Sort()
- Trying to print Sort() return value
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?Solution
Step 1: Find index of first 3
Array.IndexOf(values, 3)correctly returns the first index where 3 appears, which is 1.Step 2: Reverse the array
Array.Reverse(values);reverses the array in place as required.Final Answer:
int index = Array.IndexOf(values, 3); Array.Reverse(values); -> Option CQuick Check:
IndexOf + Reverse = int index = Array.IndexOf(values, 3); Array.Reverse(values); [OK]
- Using instance method IndexOf on array (not valid)
- Confusing FindIndex with IndexOf
- Sorting instead of reversing
Ready to prove it?
Pass the test and this pattern moves unknown
