Advertisement
Lusien_Lashans

Сортировка по числам

Jul 4th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Raspisanie
  9. {
  10.     class Sorting
  11.     {
  12.         public static void SortingByNumber(string[] stringArray)
  13.         {
  14.             int[] intArray = createIntArray(stringArray);
  15.             BubbleSort(intArray, stringArray);
  16.         }
  17.         private static void BubbleSort(int[] intArray, string[] stringArray)
  18.         {
  19.  
  20.             for (int i = 0; i < intArray.Length; i++)
  21.                 for (int j = 0; j < intArray.Length - 1; j++)
  22.                     if (intArray[j] > intArray[j + 1])
  23.                     {
  24.                         string t = stringArray[j + 1];
  25.                         stringArray[j + 1] = stringArray[j];
  26.                         stringArray[j] = t;
  27.                         int x = intArray[j + 1];
  28.                         intArray[j + 1] = intArray[j];
  29.                         intArray[j] = x;
  30.                     }
  31.         }
  32.         private static int[] createIntArray(string[] stringArray)
  33.         {
  34.             string pattern = @"\D";
  35.             Regex regex = new Regex(pattern);
  36.             int[] intArray = new int[stringArray.Length];
  37.             for (int i = 0; i < stringArray.Length; i++)
  38.             {
  39.                 string input = stringArray[i];
  40.                 string result = regex.Replace(input, "");
  41.                 intArray[i] = (result != "") ? intArray[i] = Int32.Parse(result) : intArray[i] = 0;
  42.             }
  43.             return intArray;
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement