Advertisement
Guest User

Untitled

a guest
Feb 15th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 KB | None | 0 0
  1. private void sortAlphabetticallyAscending(ListBox listBox)
  2.         {
  3.             string[] s = new string[listBox.Items.Count];
  4.             for (int i = 0; i < listBox.Items.Count; i++)
  5.                 s[i] = (string)listBox.Items[i];
  6.             Array.Sort(s);
  7.             listBox.BeginUpdate();
  8.             listBox.Items.Clear();
  9.             for (int i = 0; i < s.Length; i++)
  10.                 listBox.Items.Add(s[i]);
  11.             listBox.EndUpdate();
  12.         }
  13.  
  14.         private void sortAlphabetticallyDescending(ListBox listBox)
  15.         {
  16.             string[] s = new string[listBox.Items.Count];
  17.             for (int i = 0; i < listBox.Items.Count; i++)
  18.                 s[i] = (string)listBox.Items[i];
  19.             Array.Sort(s);
  20.             Array.Reverse(s);
  21.             listBox.BeginUpdate();
  22.             listBox.Items.Clear();
  23.             for (int i = 0; i < s.Length; i++)
  24.                 listBox.Items.Add(s[i]);
  25.             listBox.EndUpdate();
  26.         }
  27.  
  28.         private void sortByLengthAscending(ListBox listBox, int l, int r)
  29.         {
  30.             Random random = new Random();
  31.             int i = l, j = r;
  32.             string x = (string)listBox.Items[random.Next(l, r)];
  33.             while (i <= j)
  34.             {
  35.                 while (((string)listBox.Items[i]).Length < x.Length)
  36.                     i++;
  37.                 while (((string)listBox.Items[j]).Length > x.Length)
  38.                     j--;
  39.                 if (i <= j)
  40.                 {
  41.                     string temp = (string)listBox.Items[j];
  42.                     listBox.Items[j] = listBox.Items[i];
  43.                     listBox.Items[i] = temp;
  44.                     i++;
  45.                     j--;
  46.                 }
  47.             }
  48.             if (l < j)
  49.                 sortByLengthAscending(listBox, l, j);
  50.             if (r > i)
  51.                 sortByLengthAscending(listBox, i, r);
  52.         }
  53.  
  54.         private void sortByLengthDescending(ListBox listBox, int l, int r)
  55.         {
  56.             Random random = new Random();
  57.             int i = l, j = r;
  58.             string x = (string)listBox.Items[random.Next(l, r)];
  59.             while (i <= j)
  60.             {
  61.                 while (((string)listBox.Items[i]).Length > x.Length)
  62.                     i++;
  63.                 while (((string)listBox.Items[j]).Length < x.Length)
  64.                     j--;
  65.                 if (i <= j)
  66.                 {
  67.                     string temp = (string)listBox.Items[j];
  68.                     listBox.Items[j] = listBox.Items[i];
  69.                     listBox.Items[i] = temp;
  70.                     i++;
  71.                     j--;
  72.                 }
  73.             }
  74.             if (l < j)
  75.                 sortByLengthDescending(listBox, l, j);
  76.             if (r > i)
  77.                 sortByLengthDescending(listBox, i, r);
  78.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement