Sort label array with numbers

titojd

Well-known member
Joined
Oct 12, 2018
Messages
57
Programming Experience
1-3
I'm creating a label array where the Label EntryLabel creates 12 Random numbers in ascending order and an EntryText where 3 numbers are added by the user, on the click of the button the 12 numbers generated plus the 3 typed form an array of LabelResults of 15 numbers, so far Perfect,

the question is: I'm not managing to order after the ResultLabel, someone would have an idea to help me:
I'm using a BubbleSort method but I don't get a result,
where am I going wrong?
part of the code follows:

C#:
 Label[] EntradaLabel = new Label[12];
        Label[] EntradaText = new Label[3];
        Label[] ResultadoLabel = new Label[15];
        
        public frmTesteEnum()
        {
            InitializeComponent();
            GeraControles();
            gerador();
          
        }
      
        private void GeraControles()
        {
            
            int i, esquerda = 50, topo = 30;
            for (i = 0; i < 12; i++)
            {

              EntradaLabel[i] = new Label();
                EntradaLabel[i].Name = "" + i;
                EntradaLabel[i].Text = EntradaLabel[i].Name;
                EntradaLabel[i].Left = esquerda; //Defina a posição do controle no formulário
                EntradaLabel[i].Top = topo;
                EntradaLabel[i].Font = new Font("", 15, FontStyle.Bold);
                EntradaLabel[i].BorderStyle = BorderStyle.Fixed3D;
                this.Controls.Add(EntradaLabel[i]); // Adicione os controle ao formulário
                topo += (EntradaLabel[i].Height + 5);
            }

            for (i = 0; i < 3; i++)
            {

               EntradaText[i] = new Label();
                EntradaText[i].Name = " " + i;
                EntradaText[i].Text = EntradaText[i].Name;
                EntradaText[i].Left = esquerda;
                EntradaText[i].Top = topo;
                EntradaText[i].Font = new Font("", 15, FontStyle.Bold);
                EntradaText[i].BorderStyle = BorderStyle.Fixed3D;
                this.Controls.Add(EntradaText[i]);
                topo += EntradaText[i].Height + 5;


            }

            esquerda = 200;
            topo = 30;
            for (i = 0; i < 15; i++)
            {

                ResultadoLabel[i] = new Label();
                ResultadoLabel[i].Name = "label" + i;
                ResultadoLabel[i].Text = ResultadoLabel[i].Name;
                ResultadoLabel[i].Left = esquerda;
                ResultadoLabel[i].Top = topo;
                ResultadoLabel[i].Font = new Font("", 15, FontStyle.Bold);
                ResultadoLabel[i].BorderStyle = BorderStyle.FixedSingle;
                this.Controls.Add(ResultadoLabel[i]);
                topo += (ResultadoLabel[i].Height + 5);

            }
            
        }
      

        private void MostraResultado()
        {

            int i, ii = 0;

            for (i = 0; i < 12; i++)
                ResultadoLabel[i].Text = EntradaLabel[i].Text;
            for (; i < 15; i++)
                ResultadoLabel[i].Text = EntradaText[ii++].Text;

        }

      private void btnGerarNumeros_Click(object sender, EventArgs e)
        {
            //Array ResultadoLabel = Array.CreateInstance(typeof(int), new int[25]);
          
            //int[] ResultadoLabel = new int[25];
            if (EntradaText[0].Text == "" | EntradaText[1].Text == "" | EntradaText[2].Text == "")
            {
                MessageBox.Show("Por favor, escolha os tres numeros Fixos", "Adevertencia", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else
            {
                MostraResultado();

              
            }
        }
     private void gerador()
        {
            Aleatorios ale = new Aleatorios();
            int[] numerosSorteados = ale.GeradorNaoRepetidoss(12, 1, 25);

            Array.Sort(numerosSorteados);

            Control[] controles = EntradaLabel;

            for (int i = 0; i < controles.Length; i++)
            {

                int valor = numerosSorteados[i];
                controles[i].Text = valor + "";

            }
        }

// criando o método bubblesort
        public void bubblesort(int[] y)
        {
            for (int i = 1; i < y.Length; i++)
                for (int j = 0; j < y.Length - 1; j++)
                    if (y[j] > y[j + 1])
                        troca(y, j);
        }
        // algoritmo de troca de valores de variáveis:
        public void troca(int[] g, int primeiro)
        {
            int aux;
            aux = g[primeiro];
            g[primeiro] = g[primeiro + 1];
            g[primeiro + 1] = aux;
        }
 
You need to debug your code and then you tell us where you're going wrong. Set a breakpoint and step through the code line by line and confirm that each line does what you expect it to. When you find a line that doesn't, that's where you're going wrong. If you still can't work out the solution, you need to tell us all that debugging information.
 
You need to debug your code and then you tell us where you're going wrong. Set a breakpoint and step through the code line by line and confirm that each line does what you expect it to. When you find a line that doesn't, that's where you're going wrong. If you still can't work out the solution, you need to tell us all that debugging information.

if I use bubleSort(ResultLabel ), I get an error : cannot convert from 'System.Windows.Forms.Label' to 'int[]'

using array.sort(LabelResult) gives me another error: Failed to compare two elements in the array.
 
The main problem you have to get over is that the UI is not your data. (Or as those versed in the art say, the View is separate from the Model.) Your objective should be to sort the data, not the UI. All your attempts above is trying to treat the UI as if it was the data, and then trying to perform your sort operation in the UI instead of the data. The View is only used to let the user be able to interact with the Model. All the work actually happens on the Model. The UI should just reflect what is happening to the data.
 
Back
Top Bottom