.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> LINQ >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

How to integer sort a list of strings using single line of code?

Posted By:Jean Paul       Posted Date: October 27, 2010    Points: 15    Category: LINQ    URL: http://www.dotnetspark.com  

You were given numbers like "1", "2", "12" etc. which are in string format. In the normal sorting the output will be "1", "12" and "2" which is wrong. Following is the code in one line to do integer sort and display.
 

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            IList list = new List();
            list.Add("1");
            list.Add("2");
            list.Add("12");
            list.Add("4");
            list.Add("10");

            list.OrderBy(s => int.Parse(s)).ToList().ForEach(s => Console.WriteLine(s));
            Console.ReadKey(false);
        }
    }
}

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend