Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void sortAlphabetticallyAscending(ListBox listBox)
- {
- string[] s = new string[listBox.Items.Count];
- for (int i = 0; i < listBox.Items.Count; i++)
- s[i] = (string)listBox.Items[i];
- Array.Sort(s);
- listBox.BeginUpdate();
- listBox.Items.Clear();
- for (int i = 0; i < s.Length; i++)
- listBox.Items.Add(s[i]);
- listBox.EndUpdate();
- }
- private void sortAlphabetticallyDescending(ListBox listBox)
- {
- string[] s = new string[listBox.Items.Count];
- for (int i = 0; i < listBox.Items.Count; i++)
- s[i] = (string)listBox.Items[i];
- Array.Sort(s);
- Array.Reverse(s);
- listBox.BeginUpdate();
- listBox.Items.Clear();
- for (int i = 0; i < s.Length; i++)
- listBox.Items.Add(s[i]);
- listBox.EndUpdate();
- }
- private void sortByLengthAscending(ListBox listBox, int l, int r)
- {
- Random random = new Random();
- int i = l, j = r;
- string x = (string)listBox.Items[random.Next(l, r)];
- while (i <= j)
- {
- while (((string)listBox.Items[i]).Length < x.Length)
- i++;
- while (((string)listBox.Items[j]).Length > x.Length)
- j--;
- if (i <= j)
- {
- string temp = (string)listBox.Items[j];
- listBox.Items[j] = listBox.Items[i];
- listBox.Items[i] = temp;
- i++;
- j--;
- }
- }
- if (l < j)
- sortByLengthAscending(listBox, l, j);
- if (r > i)
- sortByLengthAscending(listBox, i, r);
- }
- private void sortByLengthDescending(ListBox listBox, int l, int r)
- {
- Random random = new Random();
- int i = l, j = r;
- string x = (string)listBox.Items[random.Next(l, r)];
- while (i <= j)
- {
- while (((string)listBox.Items[i]).Length > x.Length)
- i++;
- while (((string)listBox.Items[j]).Length < x.Length)
- j--;
- if (i <= j)
- {
- string temp = (string)listBox.Items[j];
- listBox.Items[j] = listBox.Items[i];
- listBox.Items[i] = temp;
- i++;
- j--;
- }
- }
- if (l < j)
- sortByLengthDescending(listBox, l, j);
- if (r > i)
- sortByLengthDescending(listBox, i, r);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement