Objects in this mirror are closer to Microsoft Technologies. DNM objective is to help users on Microsoft technologies by providing Articles, snippets, Interview Questions.

14 July 2012

sample string orderby program in .net using Orderby and OrderByDescending


We will see a sample code to generate a string order by(ascending and descending) with inbuilt string Orderby and OrderByDescending.

Eg : input is "dotnetmirror" and we need generate a output like ''deimnoorrrtt" and "ttrrroonmied"

Program
    class OrderString
    {
        static void Main()
        {
            string str = "dotnetmirror";

            foreach( char c in str.OrderBy(x =>x))
            {
                Console.Write(c);
            }
            foreach (char c in str.OrderByDescending(x => x))
            {
                Console.Write(c);
            }
            Console.ReadLine();
        }
    }

Output:
deimnoorrrtt //ascending order of the string "dotnetmirror"
ttrrroonmied //descedning order of the string "dotnetmirror"

0 Comments:

Post a Comment