How to use Array.Sort() method to sorts the elements in 1D Array in C#

2 Answers

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            String[] arr = new String[] { "hhh", "ccc", "ggg", "bbb", "eee", "aaa", "ddd" };

            Array.Sort(arr, 1, 3);

            Console.WriteLine("arr: {0}", string.Join(", ", arr));
        }
    }
}


/*
run:
     
arr: hhh, bbb, ccc, ggg, eee, aaa, ddd
    
*/

 



answered Apr 30, 2016 by avibootz
edited May 1, 2016 by avibootz
0 votes
using System;
using System.Collections;

namespace ConsoleApplication_C_Sharp
{
    public class ReverseComparer : IComparer
    {
        public int Compare(Object x, Object y)
        {
            return (new CaseInsensitiveComparer()).Compare(y, x);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            String[] arr = new String[] { "hhh", "ccc", "bbb", "ggg", "eee", "aaa", "ddd" };

            IComparer revComparer = new ReverseComparer();

            Array.Sort(arr, 1, 4, revComparer);

            Console.WriteLine("arr: {0}", string.Join(", ", arr));
        }
    }
}


/*
run:
    
arr: hhh, ggg, eee, ccc, bbb, aaa, ddd
   
*/

 



answered Apr 30, 2016 by avibootz
edited May 1, 2016 by avibootz
...