📖 Scenario: You are organizing a list of book titles for a small library. You want to sort the titles alphabetically, reverse the order, and find the position of a specific book.
🎯 Goal: Build a program that creates an array of book titles, sorts them, reverses the order, and finds the index of a given book.
📋 What You'll Learn
Create an array of strings called books with these exact titles: "C# Basics", "Learn Java", "Python Guide", "Web Development", "Data Science"
Create an integer variable called index to store the position of the book "Python Guide"
Use Array.Sort to sort the books array alphabetically
Use Array.Reverse to reverse the order of the books array
Use Array.IndexOf to find the index of "Python Guide" in the reversed array
Print the sorted array, the reversed array, and the index of "Python Guide"
💡 Why This Matters
🌍 Real World
Sorting and searching lists is common in apps like libraries, stores, and contact lists.
💼 Career
Knowing array methods helps you manage collections of data efficiently in software development.
Progress0 / 4 steps
1
Create the array of book titles
Create an array of strings called books with these exact titles: "C# Basics", "Learn Java", "Python Guide", "Web Development", "Data Science"
C Sharp (C#)
Hint
Use string[] books = { ... }; to create the array with the exact titles.
2
Create the index variable
Create an integer variable called index to store the position of the book "Python Guide". Initialize it to -1 for now.
C Sharp (C#)
Hint
Use int index = -1; to create the variable before finding the actual position.
3
Sort and reverse the array, find the index
Use Array.Sort(books) to sort the books array alphabetically. Then use Array.Reverse(books) to reverse the order. Finally, use Array.IndexOf(books, "Python Guide") to find the index of "Python Guide" in the reversed array and store it in index.
C Sharp (C#)
Hint
Call Array.Sort and Array.Reverse on books. Then find the index with Array.IndexOf.
4
Print the results
Print the sorted array, the reversed array, and the index of "Python Guide". Use Console.WriteLine and a foreach loop to print the arrays. Print the index with a descriptive message.
C Sharp (C#)
Hint
Use a foreach loop to print each book. Then print the index with Console.WriteLine.
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
Step 1: Understand Array.Sort purpose
The Array.Sort method organizes the elements of an array in ascending order by default.
Step 2: Compare with other methods
Array.Reverse flips the array, and Array.IndexOf finds an element's position, so they do different things.
Final Answer:
It arranges the items in ascending order. -> Option B
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
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.
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.
Final Answer:
Array.Reverse(numbers); -> Option D
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
C. You must use Console.WriteLine(nums.Sort()); instead.
D. The array must be converted to a list before sorting.
Solution
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).
Step 2: Understand correct usage
The correct way is Array.Sort(nums); to sort the array in place.
Final Answer:
Arrays don't have a Sort() instance method. -> Option A
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
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 C
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 unknownstrongwhile earning +1 Grip toward your certificate.