Tuesday 12 August 2014

Sort the list using linq and other ways in C#

public void MultipleWayToSortList()
            {
                List<int> i = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                i.Sort();
                foreach (var item in i)
                {
                    Console.WriteLine(item);
                }

                int[] arr = { 1, 2, 3, 4, 5, 6, 5, 4, 3, 4, 5, 6, 6, 6, 7, 7, 7, 7, 87, 88, 77, 6, 66, 6, 55, 5, 4, 444, 4, 3, 3, 3, 3, 3 };
                List<int> l = arr.ToList();
                l.Sort();
                foreach (var item in l)
                {
                    Console.WriteLine(item);
                }

                int[] arr1 = { 1, 2, 3, 4, 5, 6, 5, 4, 3, 4, 5, 6, 6, 6, 7, 7, 7, 7, 87, 88, 77, 6, 66, 6, 55, 5, 4, 444, 4, 3, 3, 3, 3, 3 };
                var a = from s
                        in arr1
                        orderby s
                        select s;
                foreach (var item in a)
                {
                    Console.WriteLine(item);
                }

            }

Output:


No comments:

Post a Comment