Sort array using Array.Sort function in C#
Nov 18, 2023 am 10:37 AMTitle: Example of using the Array.Sort function to sort an array in C
#Text:
In C#, array is a commonly used data structure , it is often necessary to sort arrays. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples.
First of all, we need to understand the basic usage of the Array.Sort function. The Array.Sort function accepts an array as a parameter and sorts the array elements in ascending order by default. If you need to sort by other specific criteria, you can pass a custom comparator function as the second parameter of Array.Sort.
Below we use a specific example to demonstrate how to use the Array.Sort function for sorting.
using System; class Program { static void Main() { // 定義一個整型數(shù)組 int[] numbers = { 5, 2, 8, 3, 1 }; // 使用Array.Sort函數(shù)對數(shù)組進行排序 Array.Sort(numbers); // 輸出排序后的數(shù)組 Console.WriteLine("排序后的數(shù)組:"); foreach (int num in numbers) { Console.WriteLine(num); } } }
In the above example, we defined an integer array numbers, containing 5 elements {5, 2, 8, 3, 1}. Next, we sort the array using the Array.Sort function. Finally, the array is traversed through a foreach loop and the sorted elements are output to the console one by one.
Run the above code, the output is as follows:
排序后的數(shù)組: 1 2 3 5 8
As you can see, the Array.Sort function sorts the array in ascending order by default.
If we need to sort the array in descending order, we can pass a custom comparator function as the second parameter of Array.Sort. The following is a sample code that sorts in descending order:
using System; class Program { static void Main() { // 定義一個整型數(shù)組 int[] numbers = { 5, 2, 8, 3, 1 }; // 使用Array.Sort函數(shù)對數(shù)組進行排序,傳遞自定義的比較器函數(shù) Array.Sort(numbers, (x, y) => y.CompareTo(x)); // 輸出排序后的數(shù)組 Console.WriteLine("按照降序排序后的數(shù)組:"); foreach (int num in numbers) { Console.WriteLine(num); } } }
In the above code, we pass a Lambda expression (x, y) => y.CompareTo(x)
as an Array The second parameter of .Sort, this expression defines a comparator function used to specify descending sorting. Running this code, the output is as follows:
按照降序排序后的數(shù)組: 8 5 3 2 1
Through the above example, we can see that arrays can be sorted easily using the Array.Sort function in C#, and different sorting methods can be specified as needed.
The above is the detailed content of Sort array using Array.Sort function in C#. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Random Number Generator in C#. Here we discuss how?Random Number Generator work, concept of pseudo-random and secure numbers.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

The history and evolution of C# and C are unique, and the future prospects are also different. 1.C was invented by BjarneStroustrup in 1983 to introduce object-oriented programming into the C language. Its evolution process includes multiple standardizations, such as C 11 introducing auto keywords and lambda expressions, C 20 introducing concepts and coroutines, and will focus on performance and system-level programming in the future. 2.C# was released by Microsoft in 2000. Combining the advantages of C and Java, its evolution focuses on simplicity and productivity. For example, C#2.0 introduced generics and C#5.0 introduced asynchronous programming, which will focus on developers' productivity and cloud computing in the future.

There are several ways to modify XML formats: manually editing with a text editor such as Notepad; automatically formatting with online or desktop XML formatting tools such as XMLbeautifier; define conversion rules using XML conversion tools such as XSLT; or parse and operate using programming languages ??such as Python. Be careful when modifying and back up the original files.

Guide to Fibonacci Series in C#. Here we discuss?Fibonacci Series Logic with different methods and how to find the Nth term.

C# multi-threaded programming is a technology that allows programs to perform multiple tasks simultaneously. It can improve program efficiency by improving performance, improving responsiveness and implementing parallel processing. While the Thread class provides a way to create threads directly, advanced tools such as Task and async/await can provide safer asynchronous operations and a cleaner code structure. Common challenges in multithreaded programming include deadlocks, race conditions, and resource leakage, which require careful design of threading models and the use of appropriate synchronization mechanisms to avoid these problems.

Methods to convert XML to JSON include: writing scripts or programs in programming languages ??(such as Python, Java, C#) to convert; pasting or uploading XML data using online tools (such as XML to JSON, Gojko's XML converter, XML online tools) and selecting JSON format output; performing conversion tasks using XML to JSON converters (such as Oxygen XML Editor, Stylus Studio, Altova XMLSpy); converting XML to JSON using XSLT stylesheets; using data integration tools (such as Informatic
