# the correct sorting of mass by weight

• There's a mass.

``````        [0] => Array
(
[title] => ITEM1
[id] => 1
[voites] => 0
)
[1] =&gt; Array
(
[title] =&gt; ITEM2
[id] =&gt; 2
[voites] =&gt; 0
)

[2] =&gt; Array
(
[title] =&gt; ITEM3
[id] =&gt; 3
[voites] =&gt; 4 // высший рейтинг
)
[3] =&gt; Array
(
[title] =&gt; ITEM4
[id] =&gt; 4
[voites] =&gt; 0
)

``````

We need to scatter the field. `voites`...and need to keep the order out of order. ♪
e.g., the normal adhesion shall first remove the element with the highest value `voites`(which makes sense), but the rest of them have the field. `0`♪ Turns out... ♪ `usort()`I can't write to him. `cmp функцию`

``````public function srtCMP(\$a, \$b){
return \$b["voites"] - \$a["voites"];
}
``````

Repeat: I need you to keep the order... the elements that have the highest ratings to rise up and the rest to leave.

Here we go.

``````        [0] => Array
(
[title] => ITEM3
[id] => 3
[voites] => 4 // высший рейтинг
)
[1] => Array
(
[title] => ITEM1
[id] => 1
[voites] => 0
)
[2] =&gt; Array
(
[title] =&gt; ITEM2
[id] =&gt; 2
[voites] =&gt; 0
)
[3] =&gt; Array
(
[title] =&gt; ITEM4
[id] =&gt; 4
[voites] =&gt; 0
)

``````

• In fact, there are two classes: `voites` and (with their equality) - by increasing the key of the element.
To apply the function `usort()` We need to temporarily remember this key inside the element.
For example:

``````\$common = Array(
"0" => Array
(
"title" => ITEM1,
"id" => 1,
"voites" => 0
),
"1" =&gt; Array
(
"title" =&gt; ITEM2,
"id" =&gt; 2,
"voites" =&gt; 0
),

"2" =&gt; Array
(
"title" =&gt; ITEM3,
"id" =&gt; 3,
"voites" =&gt; 4 // высший рейтинг
),
"3" =&gt; Array
(
"title" =&gt; ITEM4,
"id" =&gt; 4,
"voites" =&gt; 0
)

);
function cmp(\$a, \$b)
{
if (\$a["voites"] == \$b["voites"]) {
if (end(\$a) == end(\$b)) {
return 0;
}
return (end(\$a) < end(\$b)) ? -1 : 1;
}
return (\$a > \$b) ? -1 : 1;
}
array_push(\$item,\$key);
}
function delete_key(&\$item, \$key){
array_pop(\$item);
}
usort(\$common, 'cmp');
array_walk(\$common, 'delete_key');
var_dump(\$common);
``````

Result:

```array (size=4)
0 = sector
array (size=3)
'title' =state string 'ITEM3' (length=5)
'id' = constituent int 3
'voites' = constituent int 4
1 = sector
array (size=3)
'title' =state string 'ITEM1'
'id' = constituent int 1
'voites' = constituent int 0
2 = sector
array (size=3)
'title' =state string 'ITEM2' (length=5)
'id' = constituent int 2
'voites' = constituent int 0
3 = sector
array (size=3)
'title' =state string 'ITEM4' (length=5)
'id' = constituent int 4
'voites' = constituent int 0
```

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2