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] tri tableau multidimensionnel sur plusieurs champs


Sujet :

Langage PHP

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Février 2006
    Messages
    134
    Détails du profil
    Informations personnelles :
    Localisation : France, Gironde (Aquitaine)

    Informations forums :
    Inscription : Février 2006
    Messages : 134
    Points : 81
    Points
    81
    Par défaut [Tableaux] tri tableau multidimensionnel sur plusieurs champs
    bonjour.

    J'ai ecris la classe suivante pour trier un tableau contenant des tableaux sur plusieurs champs.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    $tab =
    array(
    array( 'id' => 1,'age' => 22 , 'taille' => 333 , 'nom' => 1),
    array( 'id' => 2,'age' => 22 , 'taille' =>  222 ,'nom' => 2),
    array( 'id' => 3,'age' => 21 , 'taille' =>  222 ,'nom' => 3),
    array( 'id' => 4,'age' => 21 , 'taille' =>  223 ,'nom' => 6),
    array( 'id' => 5,'age' => 21 , 'taille' =>  223 ,'nom' => 7),
    array( 'id' => 6,'age' => 21 , 'taille' =>  223 ,'nom' => 6),
    array( 'id' => 7,'age' => 21 , 'taille' =>  223 ,'nom' => 4),
    array( 'id' => 8,'age' => 22 , 'taille' =>  222 ,'nom' => 1)
    );

    Voici le code avec un exemple.

    Qu'en penssez vous, y a t il d'autres methodes pour comparer les performances?



    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
     
    class sort
    {
    	public $sorts = array();
    	public $currentSorts;
    	public $data = array();
     
    	public function __construct( $data , $key)
    	{
    		$this->sorts = $key;
    		$this->currentSorts = reset( $this->sorts );
    		$this->data = $data;
    	}
     
    	public function sort()
    	{
    		usort( $this->data , array($this, 'compare') );
    		return $this->data;
    	}
     
     
    	public function compare( $a , $b  )
    	{
    		if ( $a[$this->currentSorts] == $b[$this->currentSorts] )
    		{
    			$this->currentSorts = next( $this->sorts );
    			return $this->sort_n($a , $b);
    		}
    		return ($a[$this->currentSorts] < $b[$this->currentSorts]) ? -1 : 1;
    	}
     
    	public function sort_n( $a , $b)
    	{
    		if ( $this->currentSorts !== false )
    		{
    			if ( $a[$this->currentSorts] == $b[$this->currentSorts] )
    			{
    				$this->currentSorts = next( $this->sorts );
    				$return = $this->sort_n($a , $b);
    			}
    			else
    			{
    				$return =  ($a[$this->currentSorts] < $b[$this->currentSorts]) ? -1 : 1;
    			}
    		}
    		else 
    		{
    			$this->currentSorts = end( $this->sorts );
    			return false;
    		}
    		$this->currentSorts = prev( $this->sorts );
    		return $return;
    	}
    }
     
    $tab =
    array(
    array( 'id' => 1,'age' => 22 , 'taille' => 333 , 'nom' => 1),
    array( 'id' => 2,'age' => 22 , 'taille' =>  222 ,'nom' => 2),
    array( 'id' => 3,'age' => 21 , 'taille' =>  222 ,'nom' => 3),
    array( 'id' => 4,'age' => 21 , 'taille' =>  223 ,'nom' => 6),
    array( 'id' => 5,'age' => 21 , 'taille' =>  223 ,'nom' => 7),
    array( 'id' => 6,'age' => 21 , 'taille' =>  223 ,'nom' => 6),
    array( 'id' => 7,'age' => 21 , 'taille' =>  223 ,'nom' => 4),
    array( 'id' => 8,'age' => 22 , 'taille' =>  222 ,'nom' => 1)
    );
     
     
    $sort = new sort( $tab , array( 'age' , 'taille' , 'nom' ) );
    $resultat = $sort->sort();
    print_r( $resultat);

  2. #2
    Membre éclairé
    Avatar de genova
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    487
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 487
    Points : 790
    Points
    790
    Par défaut
    Si tu veux dans le détail, à la limite passe ton tableau par référence, ça peut pas mal jouer en terme de performances sur les gros volumes de données :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    public function __construct( &$data , $key)

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Février 2006
    Messages
    134
    Détails du profil
    Informations personnelles :
    Localisation : France, Gironde (Aquitaine)

    Informations forums :
    Inscription : Février 2006
    Messages : 134
    Points : 81
    Points
    81
    Par défaut
    Effectivement j'ai apporté cette modification.

    Sinon la methode vous parait elle efficace?

Discussions similaires

  1. Tri sur plusieurs champs d'un tableau contenant des objets
    Par swampsnake dans le forum Général JavaScript
    Réponses: 6
    Dernier message: 01/07/2011, 13h14
  2. tri tableau sur plusieurs champs
    Par manue75 dans le forum Langage
    Réponses: 3
    Dernier message: 05/06/2010, 13h08
  3. Réponses: 7
    Dernier message: 02/07/2008, 16h06
  4. [Tableaux] Comparer tableau multidimentionnel sur un champ
    Par mikebranque dans le forum Langage
    Réponses: 4
    Dernier message: 18/12/2006, 11h47
  5. [Collection] Tris sur plusieurs champs
    Par partyboy dans le forum Collection et Stream
    Réponses: 4
    Dernier message: 12/07/2005, 16h56

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