Sunday, 27 January 2013

Numeric String sort using LINQ C#

Numeric String sort using LINQ  C#


Sorting integer/decimal Strings using LINQ orderBy

            String[] salaries =
            {
 "30,000,000$","24,285,714$","23,125,000$","18,000,000$","15,729,365$",
"14,940,025$","14,000,000$","12,250,000$","10,000,000$","10,000,000$","3,200,000$",
"2,800,000$","1,875,000$","1,675,000$","1,600,000$","528,475$","527,200$",
"525,000$","523,800$","482,500$","482,000$","480,000$","480,000$"
            };


            var query = from sal in salaries
                        orderby Decimal.Parse(sal, System.Globalization.NumberStyles.Any)
                        select sal;

            foreach (String sa in query)
            {
                Console.WriteLine(sa);
            }
OUTPUT

480,000$
480,000$
482,000$
482,500$
523,800$
525,000$
527,200$
528,475$
1,600,000$
1,675,000$
1,875,000$
2,800,000$
3,200,000$
10,000,000$
10,000,000$
12,250,000$
14,000,000$
14,940,025$
15,729,365$
18,000,000$
23,125,000$
24,285,714$
30,000,000$

Tags: How to Sort Numeric Strings in C#, using LINQ to Sort Numeric Strings,LINQ orderby, LINQ to Objects, LINQ Query Expressions, LINQ Select, LINQ Orderby in asp.net,LINQ orderby in Silverlight,LINQ order by in WPF,LINQ orderby in WCF