TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# OrderBy, OrderByDescending Examples

Use OrderBy and OrderByDescending from System.Linq. Specify the value to be sorted with a lambda.
OrderByDescending. This sorts elements from high to low. It is an extension method found in the System.Linq namespace. It receives a key selector Func instance.FuncExtension
Notes, usage. We pass OrderByDescending a lambda expression that selects the key. The OrderBy method actually means OrderByAscending, as it sorts from low to high.LINQ
OrderByDescending example. Here, each Tuple has 2 items of type int. Next, we call OrderByDescending and use a lambda expression that selects the Item1 property of each Tuple instance.TupleLambda

Finally: We see that the Tuples are sorted from the highest Item1 value to the lowest Item1 value.

Info: A reverse sort (a descending sort) is performed. Using descending sorts is usually faster than using a separate reverse pass.

C# program that uses OrderByDescending using System; using System.Linq; class Program { static void Main() { // Create array of tuples. Tuple<int, int>[] tuples = new Tuple<int, int>[3]; tuples[0] = new Tuple<int, int>(3, 6); tuples[1] = new Tuple<int, int>(6, 4); tuples[2] = new Tuple<int, int>(0, 60); // Order by descending on Item1. var result = tuples.OrderByDescending(a => a.Item1); foreach (var item in result) { Console.WriteLine(item); } } } Output (6, 4) (3, 6) (0, 60)
OrderBy example. OrderBy specifies how a collection should be ordered. It is an extension method. We can invoke OrderBy on any collection that implements IEnumerable.IEnumerable

Lambda: This matches the signature of Func as the argument. On the right side of the lambda, we reverse each string to get the sort key.

ArrayString Literal

Info: We convert to an array with ToCharArray, Reverse the chars, convert to an array with ToArray, and create a string from that array.

ToCharArrayReverseToArrayString Constructor

Finally: The result (of type IOrderedEnumerable) consists of the 4 strings sorted by their characters considered in reverse order.

C# program that uses OrderBy extension method using System; using System.Linq; class Program { static void Main() { // Input array. string[] array = { "the", "glass", "bead", "game" }; // Order alphabetically by reversed strings. var result = array.OrderBy(a => new string(a.ToCharArray().Reverse().ToArray())); // Display results. foreach (var item in result) { Console.WriteLine(item); } } } Output bead the game glass
A discussion. OrderBy and OrderByDescending are often used by the C# compiler when it translates query expressions with the descending contextual keyword.Descending

Tip: Because query expressions do not require explicit delegate syntax (such as lambda expressions =>) they can be easier to read.

Research. I checked the C# specification for details about the descending keyword. When we use "descending," a call of OrderByDescending is added to the translated, method-call syntax.

Quote: If an ordering clause specifies a descending direction indicator, an invocation of OrderByDescending or ThenByDescending is produced instead (The C# Programming Language).

A summary. These 2 methods are sometimes useful. But most often they are used in the translation of query expressions with the orderby and descending keywords.Keywords
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf