C# Program to Find Integer Numbers from the List of Objects and Sort them Using LINQ
Last Updated :
06 Oct, 2022
Given a list of objects, we need to find the integer double numbers from the list of objects and sort them using LINQ. So this task can be done using the OfType() method and then the sorting of numbers can be done by using OrderBy() method. Let discuss them one by one along with their syntax:
1. OfType() method: It is used to filter the elements of an IEnumerable according to the specified type. If the given source is null then this method will throw ArgumentNullException.
Syntax:
public static System.Collections.Generic.IEnumerable<TResult> OfType<TResult>(this System.Collections.IEnumerable source);
2. OrderBy() method: This method is used to sort the elements in the collection in ascending order. If the given source is null then this method also throws ArgumentNullException.
Syntax:
public static System.Linq.IOrderedEnumerable<TSource> OrderBy<TSource,TKey(
thisSystem.Collections.Generic.IEnumerable<TSource> list, Func<TSource,TKey> Selector);
Example:
Input : ["sai", 100, "mohan", 18, 50, 200, "rajesh", 34]
Output : [18, 34, 50, 100, 200]
Input : ["raju", 345, 18.0 , "mohan", 189, 193, 30, 200, "rajesh"]
Output : [30, 189, 193, 200, 345]
Approach:
- Create an list of objects using ArrayList.
- Now using the OfType<int>() method along with OrderBy() method we will select the integer values from the list and sort the integers and then convert them into a list using ToList() method.
List<int> result = objList.OfType<int>().OrderBy(num=>num).ToList();
- Print the list using the foreach loop.
foreach (int integer in result)
{
Console.Write(integer + " ");
}
C#
// C# program to find the integer numbers from
// the list of objects and sort them.
using System;
using System.Linq;
using System.Collections.Generic;
class GFG{
static void Main(string[] args)
{
// Declaring and initializing an arrayList
// of objects
List<object> objList = new List<object>(){
"sai", 100, "mohan", 18.0, 50, 200, "rajesh", 34 };
// Selecting the integers from the list and sorting
// them using orderby()
List<int> result = objList.OfType<int>().OrderBy(num => num).ToList();
// Printing sorted list
Console.WriteLine("The Sorted integers are : ");
foreach (int integer in result)
{
Console.Write(integer + " ");
}
}
}
OutputThe Sorted integers are :
34 50 100 200
Similar Reads
C# Program to Find the Negative Double Numbers From the List of Objects Using LINQ Given a list of objects, we need to find the negative double from the list of objects, this task can be done using the OfType() method along with the Where() method. OfType() method is used to filter the elements of an IEnumerable based on a specified type. Or in other words, this method is used to
2 min read
C# Program to Sort a List of Integers Using the LINQ OrderBy() Method Given a list of integers, now our task is to sort the given list of integers. So we use the OrderBy() method of LINQ. This method is used to sort the elements in the collection in ascending order. This method is overloaded in two different ways: OrderBy<TSource, TKey>(IEnumerable<TSource
2 min read
C# Program to Find the Index of Even Numbers using LINQ Given an array, now our task is to find the index value of the even numbers present in the given array using LINQ. LINQ is known as Language Integrated Query and was introduced in .NET 3.5. It gives the power to .NET languages to generate queries to retrieve data from the data source. So to do this
2 min read
C# Program to Sort Student Names in Descending Order using LINQ Given a list of student names, now our task is to sort the names given in the list in descending order using the LINQ, This task can be done using OrderByDescending() method of LINQ. This method is used to sort the elements in the collection in descending order. While using this method in the LINQ q
2 min read
C# Program to Sort a List of String Names Using the LINQ OrderBy() Method Given a list of string names, now our task is to sort the names given in the list using the OrderBy() method of LINQ. This method is used to sort the elements in the collection in ascending order. While using this method in the LINQ query you don't need to add an extra condition to sort the list in
2 min read
C# Program to Sort a List of Employees Based on Salary using LINQ Given a list of employees, now we sort the list of employees according to their salary using LINQ. So to this task, we use the OrderBy() method. This method is used to sort the elements of the specified sequence in ascending order. Example: Input: {id = 101, Name = Rohit, Salary = 50000, Department
3 min read