linq OrderBy

linq OrderBy descending or ascending


linq OrderBy
public static void OrderBy()
        {
            string[] words = { "cherry", "apple", "blueberry" };
            var sortedWords =
                from w in words
                orderby w
                select w;

            Console.WriteLine("The sorted list of words :");
            foreach (var w in sortedWords)
            {
                Console.WriteLine("{0}", w);
            }

            var sortedWordslength = from w in words
                                    orderby w.Length
                                    select w;

            Console.WriteLine("\nThe sorted list of words (by length) :");
            foreach (var w in sortedWordslength)
            {
                Console.WriteLine("{0}", w);
            }
        }

Output


linq OrderBy Descending
public static void OrderByDescending()
        {
            double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 };

            var sortedDoubles = from d in doubles
                                orderby d descending // or ascending
                                select d;

            Console.WriteLine("The doubles from highest to lowest:");
            foreach (var d in sortedDoubles)
            {
                Console.WriteLine("{0}", d);
            }
        }
Output


linq OrderBy linq OrderBy Reviewed by Bhaumik Patel on 7:29 PM Rating: 5