C# Program to Find Integer Numbers From the List of Objects and Sort Them Using LINQ

You are currently viewing C# Program to Find Integer Numbers From the List of Objects and Sort Them Using LINQ

C# program to find integer numbers from the list of objects and sort them using LINQ

//C# Program to find integer numbers from the 
//list of objects and then sort them in ascending order 
//using Linq.

using System;
using System.Linq;
using System.Collections.Generic;

class Demo
{
    static void Main(string[] args)
    {
        List<object> objectList = new List<object>()
        {
            301, "Amit", 202,"Joseph", 805, "RK Verma", 120, "Sanjay Shukla", 407, "Pramod Pandey",405
        };

        List<int> result = objectList.OfType<int>().OrderBy(num=>num).ToList();

        Console.WriteLine("Sorted integers: ");
        foreach (int val in result)
        {
            Console.Write(val + " ");
        }
        Console.WriteLine();
    }
}

Output

Sorted integers:
120 202 301 405 407 805
Press any key to continue . . .

Ranjith Kumar

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply