Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

sorting and comparison: practical reference

sorting and comparison: practical reference: Sorting orders elements according to a consistent comparer and chosen stability.

Version: C# language and .NET 10 reference runtime

Syntax

// sorting

Sorting orders elements according to a consistent comparer and chosen stability.

Specify keys and directions explicitly; use ThenBy for subsequent keys.

An inconsistent comparer yields unpredictable results, and sorting may buffer everything.

Empty and single-element data is already sorted; null needs a defined ordering.

The complete program actually uses sorting and comparison; its exact expected stdout is 1,2,3.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

var values = new[] { 3, 1, 2 }; Array.Sort(values); Console.WriteLine(string.Join(",", values));

Result

1,2,3

The complete program actually uses `sorting and comparison`; its exact expected stdout is `1,2,3`.

Sources