Code Monkey home page Code Monkey logo

Comments (8)

NicklasWallgren avatar NicklasWallgren commented on August 11, 2024 1

This should sort by pokemon id and CP.

$pokemons = $pokebank->getPokemons();

$pokemons = $pokemons->sort(function ($a, $b) {
            if ($a->getPokemonId() != $b->getPokemonId()) {
                return ($a->getPokemonId() < $b->getPokemonId()) ? -1 : 1;
            }
            return $a->getCp() - $b->getCp();
        });

from pokemongoapi-php.

NicklasWallgren avatar NicklasWallgren commented on August 11, 2024

Alright, the PokeBank now returns a PokemonCollection on which you can sort by name, level, cp, pokedex index and IV ratio.

Have a look.

from pokemongoapi-php.

fgala avatar fgala commented on August 11, 2024

This is working just fine, tested with the Pokemon Collection

from pokemongoapi-php.

NicklasWallgren avatar NicklasWallgren commented on August 11, 2024

Resolved.

from pokemongoapi-php.

paoloalby avatar paoloalby commented on August 11, 2024

As in game, I tried to sort PokemonCollection by Pokedex Index.
In game, when I sort by pokedex index or by name, Pokemons with the same name or the same pok# are then sorted automatically by CP (desc).

Here, as we can see in the image attached, we have 3 Weedle with CP unsorted.

Is there a way to make that once we sort by name or pokedex #, it automatically sort by CP desc too?

Like in database queries: "sort by name asc, cp desc"

order by

from pokemongoapi-php.

NicklasWallgren avatar NicklasWallgren commented on August 11, 2024

Try the following snippet

$pokemons = $pokemons->sort(function (Pokemon $a, Pokemon $b) {
            if ($a->getPokemonId() != $b->getPokemonId()) {
                return ($a->getPokemonId() < $b->getPokemonId()) ? -1 : 1;
            }
            return $a->getCp() - $b->getCp();
        });

from pokemongoapi-php.

paoloalby avatar paoloalby commented on August 11, 2024

I can't make it works. I'm sure doing something wrong.

First I get the pokemons, sorted by PokedexIndex

$pokemons = $pokeBank->getPokemons()->sortByPokedexIndex();

Then I write your snippet

$pokemons = $pokemons->sort(function (Pokemon $a, Pokemon $b) {
            if ($a->getPokemonId() != $b->getPokemonId()) {
                return ($a->getPokemonId() < $b->getPokemonId()) ? -1 : 1;
            }
            return $a->getCp() - $b->getCp();
        });

But it gives me errors

Uncaught TypeError: Argument 1 passed to {closure}() must be an instance of Pokemon, instance of NicklasW\PkmGoApi\Api\Pokemon\Pokemon given

from pokemongoapi-php.

paoloalby avatar paoloalby commented on August 11, 2024

ok, now it works perfectly.

I just changed the return at the end

$pokemons = $pokemons->sort(function ($a, $b) {
            if ($a->getPokemonId() != $b->getPokemonId()) {
                return ($a->getPokemonId() < $b->getPokemonId()) ? -1 : 1;
            }
            return $b->getCp() - $a->getCp();
        });

So, I get the highest CP first

from pokemongoapi-php.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.