the correct sorting of mass by weight



  • There's a mass.

            [0] => Array
                (
                    [title] => ITEM1
                    [id] => 1
                    [voites] => 0
                )
    
        [1] => Array
            (
                [title] => ITEM2
                [id] => 2
                [voites] => 0
            )
    
        [2] => Array
            (
                [title] => ITEM3
                [id] => 3
                [voites] => 4 // высший рейтинг
            )
         [3] => Array
            (
                [title] => ITEM4
                [id] => 4
                [voites] => 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] => Array
            (
                [title] => ITEM2
                [id] => 2
                [voites] => 0
            )
         [3] => Array
            (
                [title] => ITEM4
                [id] => 4
                [voites] => 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" => Array
        (
            "title" => ITEM2,
            "id" => 2,
            "voites" => 0
        ),
    
    "2" => Array
        (
            "title" => ITEM3,
            "id" => 3,
            "voites" => 4 // высший рейтинг
        ),
     "3" => Array
        (
            "title" => ITEM4,
            "id" => 4,
            "voites" => 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;
    }

    function add_key(&$item, $key){
    array_push($item,$key);
    }

    function delete_key(&$item, $key){
    array_pop($item);
    }

    array_walk($common, 'add_key');
    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


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2