Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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# Linq ThenBy Method
Orders elements in a sequence using ThenBy() method.
We have the following string array.
string[] str = { "AAA", "AAAA", "A", "AAAAA", "AAAAAAAAA" };
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);
Example
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
string[] str = { "AAA", "AAAA", "A", "AAAAA", "AAAAAAAAA" };
IEnumerable<string> res = str.AsQueryable() .OrderBy(alp => alp.Length).ThenBy(alp => alp);
foreach (string arr in res)
Console.WriteLine(arr);
}
}
Output
A AAA AAAA AAAAA AAAAAAAAA
Advertisements