Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to list value in array from biggest to smallest

Comments in 'Plugin Development' started by wolfdale, May 16, 2015.

  1. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    For example there is a array $this->kills
    inside is
    A => 10
    B=> 5
    C=>7
    which are the number of kills

    how do i broadcast like:

    A is the winner!
    C is 2nd !
    B is 3rd !
  2. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,707
    Plugins:
    22
    Minecraft User:
    Falkirknh
    You can use arsort()
    PHP:
    arsort($this->kills); // The "natural" order is now from high -> low
    $names array_keys($this->kills); // Array of names from top kills to least kills
    $names[0]; // Best player
    $names[count($names)-1]; // Worst player
    Here is something close to what you want (untested)
    PHP:
    arsort($this->kills);
    $names array_keys($this->kills);
    $this->getServer()->broadcastMessage($names[0] . " is the winner!");
    for(
    $i 1$i count($names); $i++){
        
    $place $i 1;
        
    $last $place 10;
        
    $lastTwo $place 100;
        if(
    $lastTwo 10 && $lastTwo 20$this->getServer()->broadcastMessage($names[$i] . " is " $place "th!");
        elseif(
    $last 10 == 1$this->getServer()->broadcastMessage($names[$i] . " is " $place "st!");
        elseif(
    $last 10 == 2$this->getServer()->broadcastMessage($names[$i] . " is " $place "nd!");
        elseif(
    $last 10 == 3$this->getServer()->broadcastMessage($names[$i] . " is " $place "rd!");
        else 
    $this->getServer()->broadcastMessage($names[$i] . " is " $place "th!");
    }

    Further reading on PHP array sorting: http://php.net/manual/en/array.sorting.php
    Further reading on sorting: https://en.wikipedia.org/wiki/Sorting_algorithm

    EDIT: If you want to be efficient, every time an update is made, you should run an insertion step (Insertion sort) on the updated item. Then again, that might not help much. It depends on how many times the same data is broadcasted.
    Last edited: May 17, 2015
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Or better way, use an SQLite3 or MySQL database, and use SELECT ... FROM kills ORDER BY *** DESC LIMIT ***.
    wolfdale and Falk like this.
  4. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    $sorted = arsort($this->kills); seems to return a bootlean (true)
    PEMapModder likes this.
  5. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    I did a little research and fixed it, thank you so much Falk and PEMapModder !!
  6. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,707
    Plugins:
    22
    Minecraft User:
    Falkirknh
    I updated my answer.
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    arsort() sorts the array passed. The array is by-reference changed, and the return value of arsort() usually doesn't help you.

Share This Page

Advertisement