
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
C# Program to order array elements
Use ThenBy() method to order array elements. Let’s say we have the following string array.
string[] str = { "Sandler", "Jack", "Tom", "Matt", "Henry", "Johnny" };
Now, use Lambda Expressions and set a condition inside the ThenBy() method to sort the strings according to the number of characters they have.
IEnumerable<string> res = str.AsQueryable().OrderBy(alp => alp.Length).ThenBy(alp => alp);
Here is the entire example to order array elements using ThenBy() method.
Example
using System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { string[] str = { "Sandler", "Jack", "Tom", "Matt", "Henry", "Johnny" }; IEnumerable<string> res = str.AsQueryable().OrderBy(alp => alp.Length).ThenBy(alp => alp); foreach (string arr in res) Console.WriteLine(arr); } }
Output
Tom Jack Matt Henry Johnny Sandler
- Related Articles
- C# Program to order array elements in descending order
- Python program to print the elements of an array in reverse order
- Python program to sort the elements of an array in ascending order
- Python program to sort the elements of an array in descending order
- Golang Program To Sort The Elements Of An Array In Ascending Order
- Golang Program To Sort The Elements Of An Array In Descending Order
- Swift Program to Sort the Elements of an Array in Ascending Order
- Swift Program to Sort the Elements of an Array in Descending Order
- C++ Program to Sort the Elements of an Array in Descending Order
- C++ Program to Sort the Elements of an Array in Ascending Order
- JavaScript Program to Find k maximum elements of array in original order
- C program to sort an array of ten elements in an ascending order
- C++ Program to Sort Elements in Lexicographical Order (Dictionary Order)
- Java Program to Sort Elements in Lexicographical Order (Dictionary Order)
- Swift Program to Sort Elements in Lexicographical Order (Dictionary Order)

Advertisements