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?
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.
– Rodrigo Segatto
Right, so you can make a simple
$search = $this->NomeModel->find('all' array('order' => $param))
and return$search
where$param
is similar tonome DESC
– Julyano Felipe
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..
– Julyano Felipe
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.
– Rodrigo Segatto
Have you tried removing the
{s}
in front of theQuantidade
?– Julyano Felipe
Yes, but it doesn’t work either. I’ll post down here another way that could help me.
– Rodrigo Segatto
Let’s go continue this discussion in chat.
– Julyano Felipe
Maybe if you stayed like this you could help me: ideone.com/Mkxrqx
– Rodrigo Segatto