IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

[Tableaux] Trier un array [FAQ]


Sujet :

Langage PHP

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2002
    Messages
    102
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Avril 2002
    Messages : 102
    Points : 70
    Points
    70
    Par défaut [Tableaux] Trier un array
    Bonjour !

    J'aimerai trier un arry 2 dimensions en fonction du nom de la colonne. Existe-il une méthode ou est-ce que je dois l'écrire moi ?

    Voici comment je rempli mon tableau via une boucle :

    tabStats[$i]['nbPenalitesChamp'] = $nbPenChamp ;
    tabStats[$i]['nbPenalitesTourn'] = $nbPenTourn ;
    tabStats[$i]['nbPenalitesTotal'] = $nbPenChamp + $nbPenTourn ;

    tabStats[$i]['nbPenalitesMatchChamp'] = $nbPenMatchChamp ;
    tabStats[$i]['nbPenalitesMatchTourn'] = $nbPenMatchTourn ;
    tabStats[$i]['nbPenalitesMatchTotal'] = $nbPenMatchChamp +$nbPenMatchTourn ;

  2. #2
    Membre actif Avatar de renaudjuif
    Inscrit en
    Avril 2006
    Messages
    325
    Détails du profil
    Informations forums :
    Inscription : Avril 2006
    Messages : 325
    Points : 258
    Points
    258
    Par défaut
    as tu été voir du côté de array_multisort ?
    http://es2.php.net/manual/fr/functio...-multisort.php

  3. #3
    Membre expérimenté
    Avatar de Anduriel
    Homme Profil pro
    Ingénieur intégration
    Inscrit en
    Février 2004
    Messages
    2 290
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur intégration

    Informations forums :
    Inscription : Février 2004
    Messages : 2 290
    Points : 1 500
    Points
    1 500
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    <?php
       sort($tabStats[$i], SORT_STRING);
    ?>

  4. #4
    Expert éminent Avatar de Mr N.
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    5 418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 5 418
    Points : 6 449
    Points
    6 449
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    function compare($a, $b) {
       return strnatcasecmp($a['nbPenalitesMatchChamp'], $b['nbPenalitesMatchChamp']);
    }
    usort(tabStats, 'compare');

  5. #5
    Membre expérimenté
    Avatar de Anduriel
    Homme Profil pro
    Ingénieur intégration
    Inscrit en
    Février 2004
    Messages
    2 290
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur intégration

    Informations forums :
    Inscription : Février 2004
    Messages : 2 290
    Points : 1 500
    Points
    1 500
    Par défaut
    J'ai remarqué que tu aimais bien usort()

  6. #6
    Expert éminent Avatar de Mr N.
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    5 418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 5 418
    Points : 6 449
    Points
    6 449
    Par défaut
    Ben c'est extremement simple et pratique et j'ai jamais chercher à comprendre comment utiliser array_multisort

  7. #7
    Membre actif Avatar de renaudjuif
    Inscrit en
    Avril 2006
    Messages
    325
    Détails du profil
    Informations forums :
    Inscription : Avril 2006
    Messages : 325
    Points : 258
    Points
    258
    Par défaut
    array_multisort est vraiment super pratique et je n'utilise pratiquement que ça pour les tris / manipulations de tableaux multi dimensionnels

  8. #8
    Expert éminent Avatar de Mr N.
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    5 418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 5 418
    Points : 6 449
    Points
    6 449
    Par défaut
    Pour satisfaire ma curiosité, pourrais-tu donner un exemple équivalent à la solution que j'ai proposé, avec array_multisort ?

  9. #9
    Membre actif Avatar de renaudjuif
    Inscrit en
    Avril 2006
    Messages
    325
    Détails du profil
    Informations forums :
    Inscription : Avril 2006
    Messages : 325
    Points : 258
    Points
    258
    Par défaut
    Effectivement, le array_multisort n'est pas forcément adapté au pb posé..
    j'ai vu "2 dimensions", j'ai donc pensé multisort... et c'est pour ça que j'ai écrit d'aller "voir" le multisort. c'était une piste sans avoir plus creusé le problème..

  10. #10
    Expert éminent Avatar de Mr N.
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    5 418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 5 418
    Points : 6 449
    Points
    6 449
    Par défaut
    Ben dans la doc il montre un exemple (n° 3) pour trier un tableau comme on le voudrait. Si j'ai bien compris il faudrait faire comme ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    foreach($tabStats as $stat) {
       $colonne_a_trier[] = $stat['colonne_a_trier'];
    }
    array_multisort($colonne_a_trier, SORT_DESC, $tabStats);
    J'ai juste ? Si oui je ne trouve pas ça terrible si il faut faire une boucle au préalable pour pouvoir trier un tableau

  11. #11
    Membre actif Avatar de renaudjuif
    Inscrit en
    Avril 2006
    Messages
    325
    Détails du profil
    Informations forums :
    Inscription : Avril 2006
    Messages : 325
    Points : 258
    Points
    258
    Par défaut
    ca ferait une boucle comme ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    foreach ($tabStats as $key => $row) {
      $nbPenalitesMatchChamp[$key]  = $row['nbPenalitesMatchChamp'];
    }
    array_multisort($nbPenalitesMatchChamp, SORT_ASC, $tabStats);
    c'est vrai qu'il y a 1 boucle mais il y a 1 appel de fonction en moins (fonction compare).

    Et au niveau performance j'ai fait 1 test pour voir (avec microtime) sur une boucle avec même tri pour usort et array_multisort:

    temps d'exécution usort : 36.67
    et le temps avec multisort: 10.96

    Etonnant, non ?

  12. #12
    Expert éminent Avatar de Mr N.
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    5 418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 5 418
    Points : 6 449
    Points
    6 449
    Par défaut
    En effet. j'aurais pensé qu'un parcours de tableau serait plus pénalisant. Merci pour l'info

Discussions similaires

  1. [Tableaux] trier plusieurs array
    Par djedje37et28 dans le forum Langage
    Réponses: 5
    Dernier message: 29/03/2007, 10h44
  2. [Tableaux] Comment trier un array multidimensionel ?
    Par kaptnkill dans le forum Langage
    Réponses: 2
    Dernier message: 26/09/2006, 09h31
  3. [Tableaux] Trier un tableau [array]
    Par clemsouz dans le forum Langage
    Réponses: 2
    Dernier message: 15/05/2006, 13h33
  4. [Tableaux] Trier un tableau comme avec ORDER BY DESC
    Par Anduriel dans le forum Langage
    Réponses: 28
    Dernier message: 08/12/2005, 18h50
  5. [Tableaux] Gérer une array...
    Par kult dans le forum Langage
    Réponses: 7
    Dernier message: 16/11/2005, 17h03

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo