DevAcademia
C++C#CPythonJava
  • C# Basics

  • C# Introduction
  • C# Get Started
  • C# Syntax
  • C# Output
  • C# Comments
  • C# Variables
  • C# Data Types
  • C# Type Casting
  • C# User Input
  • C# Operators
  • C# Math
  • C# Strings
  • C# Booleans
  • C# If...Else
  • C# Switch Statement
  • C# While Loop
  • C# For Loop
  • C# Break and Continue
  • C# Arrays
  • C# Files
  • C# OOP

  • C# OOP Introduction
  • C# Classes and Objects
  • C# Class Members
  • C# Constructors
  • C# Destructors
  • C# Access Modifiers
  • C# Properties
  • C# Inheritance
  • C# Polymorphism
  • C# Abstraction
  • C# Interfaces
  • C# Enums
  • C# Exceptions
  • C# Quizzes

  • C# Quiz Introduction
  • C# Basics

  • C# Introduction
  • C# Get Started
  • C# Syntax
  • C# Output
  • C# Comments
  • C# Variables
  • C# Data Types
  • C# Type Casting
  • C# User Input
  • C# Operators
  • C# Math
  • C# Strings
  • C# Booleans
  • C# If...Else
  • C# Switch Statement
  • C# While Loop
  • C# For Loop
  • C# Break and Continue
  • C# Arrays
  • C# Files
  • C# OOP

  • C# OOP Introduction
  • C# Classes and Objects
  • C# Class Members
  • C# Constructors
  • C# Destructors
  • C# Access Modifiers
  • C# Properties
  • C# Inheritance
  • C# Polymorphism
  • C# Abstraction
  • C# Interfaces
  • C# Enums
  • C# Exceptions
  • C# Quizzes

  • C# Quiz Introduction

Loading Cs tutorial…

Loading content
C# BasicsTopic 36 of 55
←PreviousPrevNextNext→

C# Sort Arrays

Introduction to Sorting Arrays

Sorting is the process of arranging elements in a specific order, such as ascending or descending. It is one of the most common operations performed on collections of data.

In C#, arrays can be sorted easily using built-in methods from the System namespace, most commonly Array.Sort().

Sorting with Array.Sort()

The Array.Sort() method sorts the elements of an array in ascending order by default.

It works with numeric arrays, strings, and other types that implement IComparable.

Example
using System;

namespace SortArrayExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = { 5, 2, 9, 1, 5, 6 };
            Array.Sort(numbers);

            Console.WriteLine("Sorted array:");
            foreach (int num in numbers)
            {
                Console.Write(num + " ");
            }
        }
    }
}
Output
Sorted array:
1 2 5 5 6 9

Sorting in Descending Order

To sort in descending order, you can call Array.Sort() to sort ascending and then use Array.Reverse() to flip the order.

This approach works for numbers, strings, or any comparable data type.

Example
using System;

namespace SortDescendingExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] names = { "David", "Alice", "Bob" };
            Array.Sort(names);
            Array.Reverse(names);

            Console.WriteLine("Sorted names in descending order:");
            foreach (string name in names)
            {
                Console.WriteLine(name);
            }
        }
    }
}
Output
Sorted names in descending order:
David
Bob
Alice
Test your knowledge: C# Sort Arrays
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
C# BasicsTopic 36 of 55
←PreviousPrevNextNext→