Esempio n. 1
0
    static void Main(string[] args)
    {
        // You are given an array of strings. Write a method that sorts the array by the length
        // of its elements (the number of characters composing them).

        Console.Write("Insert N = ");
        int N = int.Parse(Console.ReadLine());

        string[] arrayOfStrings = new string[N];

        for (int i = 0; i < arrayOfStrings.Length; i++)
        {
            Console.Write("arr[" + i + "] = ");
            arrayOfStrings[i] = Console.ReadLine();
        }

        LengthComparer comparator = new LengthComparer();

        Array.Sort(arrayOfStrings, comparator);

        foreach (string value in arrayOfStrings)
        {
            Console.WriteLine(value);
        }
    }
Esempio n. 2
0
    static void Main()
    {
        Console.Write("Enter the length of the array: ");
        int length = int.Parse(Console.ReadLine());

        string[] array = new string[length];

        for (int i = 0; i < length; i++)
        {
            array[i] = Console.ReadLine();
        }

        //with IComparer
        LengthComparer comparer = new LengthComparer();

        Array.Sort(array, comparer);

        string result = string.Join(", ", array);

        Console.WriteLine(result);

        //another solution

        // Array.Sort(array, (x, y) => x.Length.CompareTo(y.Length));
        // string result = string.Join(", ", array);
        // Console.WriteLine(result);
    }
Esempio n. 3
0
        /// <summary>
        /// Sorts the list of strings so the longest names are in the start of the list
        /// When there is an image named "a.aspx?id=1" and another named "a.aspx?id=12", the list should start with "a.aspx?id=12"
        /// This guarantees that string replacement will not damage the names
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static private List <string> SortNoDuplicate(List <string> input)
        {
            List <string>      result   = new List <string>(input);
            IComparer <string> comparer = new LengthComparer();

            result.Sort(comparer);
            return(result);
        }
Esempio n. 4
0
        private static void Main(string[] args)
        {
            // Ask the class library where the user's My Documents folder lives
            string path =
                Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            string[] files    = Directory.GetFiles(path);
            var      comparer = new LengthComparer();

            Array.Sort(files, comparer);
            foreach (string file in files)
            {
                Console.WriteLine(file);
            }
        }
        public void AddMethod_StringWithCustomComparer_BinaryTree()
        {
            var comparer = new LengthComparer();
            BinarySearchTree <string> tree   = new BinarySearchTree <string>(new string[] { "assert", "new", "know", "knowledge", "yes" }, comparer);
            List <string>             actual = new List <string>();

            foreach (var item in tree)
            {
                actual.Add(item);
            }

            List <string> expected = new List <string>()
            {
                "new", "yes", "know", "assert", "knowledge"
            };

            Assert.AreEqual(expected, actual);
        }
Esempio n. 6
0
 public void SetUp()
 {
     comparer = new LengthComparer(SortDirection.Ascending);
 }
Esempio n. 7
0
        static void Main(string[] args)
        {
            MyList <int> lst = new MyList <int>();

            Console.WriteLine("---------------Adding---------------");
            lst.Add(1);
            lst.Add(2);
            lst.Add(3);
            lst.Add(4);

            print(lst);

            Console.WriteLine("---------------Clear---------------");
            lst.Clear();
            print(lst);

            lst.Add(11);
            lst.Add(12);
            lst.Add(13);
            lst.Add(14);
            lst.Add(10);
            print(lst);


            Console.WriteLine();
            Console.WriteLine("---------------Contains---------------");
            Console.WriteLine(lst.Contains(11));
            Console.WriteLine(lst.Contains(1));


            Console.WriteLine("---------------Copy to---------------");
            int[] arr = new int[7];
            lst.CopyTo(arr, 0);

            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write($"{arr[i]} ");
            }
            Console.WriteLine();


            Console.WriteLine("---------------Copy to---------------");
            int[] arr2 = new int[5];
            lst.CopyTo(arr2);

            for (int i = 0; i < arr2.Length; i++)
            {
                Console.Write($"{arr2[i]} ");
            }
            Console.WriteLine();


            Console.WriteLine("---------------Copy to---------------");
            int[] arr3 = new int[10];
            lst.CopyTo(1, arr3, 2, 2);

            for (int i = 0; i < arr3.Length; i++)
            {
                Console.Write($"{arr3[i]} ");
            }
            Console.WriteLine();

            Console.WriteLine("---------------Contains---------------");
            Console.WriteLine(lst.Remove(13));
            print(lst);

            Console.WriteLine("---------------Index of---------------");
            Console.WriteLine(lst.IndexOf(10));
            Console.WriteLine(lst.IndexOf(13));
            print(lst);

            Console.WriteLine("---------------Insert---------------");
            lst.Insert(0, 20);
            print(lst);

            Console.WriteLine("---------------Remove at---------------");
            lst.RemoveAt(2);
            print(lst);


            Console.WriteLine("---------------Sort---------------");
            lst.Sort();
            print(lst);


            Console.WriteLine("---------------Event---------------");
            lst.AddEvent(100, 1);
            print(lst);
            lst.AddEvent(200);
            print(lst);


            Console.WriteLine("-----------------------------Rectangle list-----------------------------");

            MyList <Rectangle> rectangles     = new MyList <Rectangle>();
            Rectangle          r1             = new Rectangle(11, 20);
            Rectangle          r2             = new Rectangle(12, 13);
            Rectangle          r3             = new Rectangle(10, 10);
            LengthComparer     lengthComparer = new LengthComparer();

            rectangles.Add(r1);
            rectangles.Add(r2);
            rectangles.Add(r3);

            Console.WriteLine("---------------Sort---------------");
            rectangles.Sort();

            foreach (var value in rectangles)
            {
                Console.WriteLine($"{value.length}, {value.width}");
            }

            Console.WriteLine("---------------Sort---------------");
            rectangles.Sort(lengthComparer);

            foreach (var value in rectangles)
            {
                Console.WriteLine($"{value.length}, {value.width}");
            }


            Console.WriteLine("---------------Event---------------");

            lst.updateItemEvent += (source, e) =>
            {
                Console.WriteLine($"Update item - {e.Index}, old item - {e.OldItem}, new Item - {e.NewItem}");
            };


            lst.Update(1, 250);
            Console.ReadLine();
        }