Advertisement
Guest User

Custom Comparator

a guest
Jun 13th, 2016
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Runtime.Remoting.Metadata.W3cXsd2001;
  6.  
  7. class Program
  8. {
  9.     static void Main(string[] args)
  10.     {
  11.         long[] numbers = Console.ReadLine().Split(new char[] { }, StringSplitOptions.RemoveEmptyEntries).Select(long.Parse).ToArray();
  12.  
  13.         Comparer<long> evenOddSorter = Comparer<long>.Create((x1, x2) =>
  14.         {
  15.             if (Math.Abs(x1) % 2 == 0 && Math.Abs(x2) % 2 == 1)
  16.                 return -1;
  17.             else if (Math.Abs(x1)%2 == 1 && Math.Abs(x2)%2 == 0)
  18.                 return 1;
  19.             else
  20.                 return 0;
  21.         });
  22.  
  23.  
  24.         Array.Sort(numbers, evenOddSorter);
  25.  
  26.         Console.WriteLine(String.Join(" ", numbers));
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement