How to find the integers from a list of objects and sort them using Linq in C#

1 Answer

0 votes
using System;
using System.Linq;
using System.Collections.Generic;
  
class Program
{
    static void Main() {
        var list = new List<object> { 3, 7, "c#", 8, "c", 99, 10, "c++", -5, 1, "java", 2 };

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

        Console.WriteLine(String.Join(", ", result));
    }
}
  
  
  
/*
run:
     
-5, 1, 2, 3, 7, 8, 10, 99
   
*/
   

 



answered Sep 27, 2023 by avibootz
...