Seal_of_approval

task1(b)

Apr 22nd, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.89 KB | None | 0 0
  1. using System;
  2.  
  3. namespace task1
  4. {
  5.     class Program
  6.     {
  7.         delegate bool Function<T>(T a, T b);
  8.  
  9.         static bool StringComparator(string a, string b)
  10.         {
  11.             if (a.Length != b.Length)
  12.                 return a.Length > b.Length;
  13.             else
  14.             {
  15.                 for (int i = 0; i < a.Length; i++)
  16.                 {
  17.                     if (a [i] != b [i])
  18.                         return a [i] > b [i];
  19.                 }
  20.                 return true;
  21.             }
  22.         }
  23.  
  24.         static T[] Sort<T>(T[] a, Function<T> comparator)
  25.         {
  26.             for (int i = 0; i < a.Length - 1; i++)
  27.                 for (int j = i + 1; j < a.Length; j++)
  28.                 {
  29.                     if (comparator (a [i], a [j]))
  30.                     {
  31.                         T tmp = a [i];
  32.                         a [i] = a [j];
  33.                         a [j] = tmp;
  34.                     }
  35.                 }
  36.             return a;
  37.         }
  38.  
  39.         public static void Main (string[] args)
  40.         {
  41.             String[] arr = {"abc", "aaa", "ascd", "aabc"};
  42.             String[] a = Sort (arr, StringComparator);
  43.  
  44.             for (int i = 0; i < a.Length; i++)
  45.                 Console.Write("{0} ", a[i]);
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment