

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- C# Program to order array elements in descending order
- C++ Program to Sort Elements in Lexicographical Order (Dictionary Order)
- Java Program to Sort Elements in Lexicographical Order (Dictionary 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
- MongoDB query to change order of array elements?
- C program to sort an array of ten elements in an ascending order
- How to sort Java array elements in ascending order?
- Program to print matrix elements in spiral order in python
- Print the last occurrence of elements in array in relative order in C Program.
- C++ program to rearrange all elements of array which are multiples of x in increasing order
- 8086 program to determine modulus of first array elements corresponding to another array elements
- C program to reverse an array elements
- C# program to sort an array in descending order
Advertisements