Sep::Sort Cakephp array

Asked

Viewed 180 times

0

I need to sort an array in cakephp according to the order passed by parameter.

The code that makes the ordering would be this:

 //Realizar a ordenação de acordo com o que receber do formulário no ajax

    if ($this->request->data['Advertencia']['campo_ordem'] == 'Funcionario.quantidade|asc') {
        $retornoFinal = Set::sort($retornoFinal, '{n}.{s}.Funcionario.quantidade', 'asc');
    }else if($this->request->data['Advertencia']['campo_ordem'] == 'Funcionario.quantidade|desc'){
        $retornoFinal = Set::sort($retornoFinal, '{n}.{s}.Funcionario.quantidade', 'desc');
    }else if($this->request->data['Advertencia']['campo_ordem'] == 'Funcionario.matricula|asc'){
        $retornoFinal = Set::sort($retornoFinal, '{n}.{s}.Funcionario.matricula', 'asc');
    }else if($this->request->data['Advertencia']['campo_ordem'] == 'Funcionario.matricula|desc'){
        $retornoFinal = Set::sort($retornoFinal, '{n}.{s}.Funcionario.matricula', 'desc');
    }else if($this->request->data['Advertencia']['campo_ordem'] == 'Funcionario.nome|desc'){
        $retornoFinal = Set::sort($retornoFinal, '{n}.{s}.Funcionario.nome', 'desc');
    }else{
        $retornoFinal = Set::sort($retornoFinal, '{n}.{s}.Funcionario.nome', 'asc');
    } 

The array is initially in this format:

Array
(
[5307] => Array
    (
        [A] => Array
            (
                [Funcionario] => Array
                    (
                        [quantidade] => 2
                        [matricula] => 5307
                        [nome] => DIOMAR 
                        [tipo_punicao] => A
                    )

            )

        [CA] => Array
            (
                [Funcionario] => Array
                    (
                        [quantidade] => 4
                        [matricula] => 5307
                        [nome] => DIOMAR 
                        [tipo_punicao] => CA
                    )

            )

        [RC] => Array
            (
                [Funcionario] => Array
                    (
                        [quantidade] => 4
                        [matricula] => 5307
                        [nome] => DIOMAR 
                        [tipo_punicao] => RC
                    )

            )

        [SR] => Array
            (
                [Funcionario] => Array
                    (
                        [quantidade] => 2
                        [matricula] => 5307
                        [nome] => 
                        [tipo_punicao] => SR
                    )

            )

    )

[5401] => Array
    (
        [A] => Array
            (
                [Funcionario] => Array
                    (
                        [quantidade] => 2
                        [matricula] => 5401
                        [nome] => VALDOMIRO 
                        [tipo_punicao] => A
                    )

            )

        [CA] => Array
            (
                [Funcionario] => Array
                    (
                        [quantidade] => 1
                        [matricula] => 5401
                        [nome] => VALDOMIRO 
                        [tipo_punicao] => CA
                    )

            )

        [RC] => Array
            (
                [Funcionario] => Array
                    (
                        [quantidade] => 2
                        [matricula] => 5401
                        [nome] => VALDOMIRO 
                        [tipo_punicao] => RC
                    )

            )

    )
)

When you enter Ifs that sort by 'Name' or 'Matricula' it works correctly, but by 'QUANTITY' it doesn’t work and this is the most important. It is possible to do otherwise or tidy up?

1 answer

0

I don’t have the reputation to comment on your question, so I’ll write here. Is this ordering that you want to do visual (for the user) only or does it have to be ordered in the array as well? If it is visual only use via Javascript ;)

  • I need you to sort in the array, really. I understand that it could be done by java script, but in this case it does not apply.

  • Right, so you can make a simple $search = $this->NomeModel->find('all' array('order' => $param)) and return $search where $param is similar to nome DESC

  • But the way I mentioned it, it can be very slow if there is a lot of data... But it is something that works.. I saw that there is a function in Cake to sort the array without having to make multiple requests.. I’ll take a look and test and anything else I command here..

  • It’s a little more complicated than what I’m doing. This array I passed contains values, like QUANTITY, that don’t come from Model. It was calculated and inserted in the array and need to draw this value by Decreasing and Increasing Quantity. The strange thing is that the way it is, it works correctly for 'Name', 'Enrollment', but not for quantity.

  • Have you tried removing the {s} in front of the Quantidade?

  • Yes, but it doesn’t work either. I’ll post down here another way that could help me.

  • Maybe if you stayed like this you could help me: ideone.com/Mkxrqx

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.