using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ch7_5
{
    public partial class Form1 : Form
    {
        private int[] array = new int[10];
        //delegate implementation for ascending sort
        private bool ascendingSort(int e1, int e2)
        {
            return e1 > e2;
        }
        private bool descendingSort(int e1, int e2)
        {
            return e1 < e2;
        }
        private void display()
        {
            listBox2.Items.Clear();
            foreach (int element in array)
            {
                listBox2.Items.Add(element);
            }
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void createButton_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            listBox2.Items.Clear();
            Random num = new Random();
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = num.Next(100);
                listBox1.Items.Add (array[i] );
            }
        }

        private void ascendingButton_Click(object sender, EventArgs e)
        {
            //sort array, passing delegate for ascending sort
            DelegateBubleSort.sortArray(array,
                 new DelegateBubleSort.Comparator(ascendingSort));
            display();
        }

        private void descendingButton_Click(object sender, EventArgs e)
        {
            DelegateBubleSort.sortArray(array,
                 new DelegateBubleSort.Comparator(descendingSort));
            display();
        }
    }
}
