14,9 → 14,9 |
*/ |
// TODO : réaliser une seule méthode pour trierMD et trierMDType |
class Tableau { |
const TRIE_NATUREL = 'nat'; |
const TRIE_CASSE_SENSIBLE = 'ci'; |
const TRIE_CASSE_INSENSIBLE = 'ci'; |
const TRI_NATUREL = 'nat'; |
const TRI_CASSE_SENSIBLE = 'ci'; |
const TRI_CASSE_INSENSIBLE = 'ci'; |
|
private static $triMultiDimension = null; |
private static $triType = null; |
76,7 → 76,10 |
$params = array(); |
foreach ($cols as $col => $order) { |
$params[] =& $colarr[$col]; |
$params = array_merge($params, (array) $order); |
$orders = (array) $order; |
foreach($orders as $orderElement) { |
$params[] =& $orderElement; |
} |
} |
call_user_func_array('array_multisort', $params); |
$ret = array(); |
99,45 → 102,48 |
} |
|
/** |
* Méthode réalisant un tri d'un tableau multidimension. |
* Méthode réalisant un tri d'un tableau multidimension. Attention les clés du tableau ne sont pas modifiées. |
* A utiliser de cette façon: |
* EfloreTriage::trieMultiple( $tableau_multidimension, array('ma_cle1', 'ma_cle1_ordre_tri', 'ma_cle2', 'ma_cle2_ordre_tri'), $type_de_tri); |
* EfloreTriage::trieMultiple( $tableau_multidimension, array('ma_cle1' => SORT_ASC, 'ma_cle2' => SORT_DESC), $type_de_tri); |
* Utiliser les constantes php SORT_DESC ou SORT_ASC pour l'odre de tri. |
* Pour le type de tri : utiliser Tableau::TRIE_NATUREL pour un trie naturel, Tableau::TRIE_CASSE_SENSIBLE pour tri sensible à la casse ou Tableau::TRIE_CASSE_INSENSIBLE pour insensible. |
* Pour le type de tri : utiliser : |
* - Tableau::TRI_NATUREL pour un trie naturel, |
* - Tableau::TRI_CASSE_SENSIBLE pour un tri sensible à la casse, |
* - Tableau::TRI_CASSE_INSENSIBLE pour un tri insensible à la casse. |
* @param array le tableau à trier |
* @param array le talbeau des colonnes à trier constituer d'un suite de nom de clé de tableau et d'ordre de tri. |
* @param array le talbeau des colonnes à trier constituer de nom de clé en clé et d'ordres de tri en valeur. |
* @param string le type de tri à appliquer. |
* @return array le tableau trié. |
*/ |
public static function trierMDType(&$tableau, $cols, $type = self::TRIE_CASSE_INSENSIBLE) { |
public static function trierMDType(&$tableau, $cols, $type = self::TRI_CASSE_INSENSIBLE) { |
self::$triMultiDimension = $cols; |
self::$triType = $type; |
usort($tableau, array('self', 'comparer')); |
uasort($tableau, array('self', 'comparer')); |
return $tableau; |
} |
|
private static function comparer($a, $b) { |
$cols = self::$triMultiDimension; |
$i = 0; |
$resultat = 0; |
$colonne_nbre = count($cols); |
while ($resultat == 0 && $i < $colonne_nbre) { |
$mot_01 = Chaine::supprimerAccents($b[$cols[$i]]); |
$mot_02 = Chaine::supprimerAccents($a[$cols[$i]]); |
switch (self::$triType) { |
case self::TRIE_NATUREL : |
$resultat = ($cols[$i + 1] == SORT_DESC) ? strnatcmp($mot_01, $mot_02) : strnatcmp($mot_02, $mot_01); |
break; |
case self::TRIE_CASSE_SENSIBLE : |
$resultat = ($cols[$i + 1] == SORT_DESC) ? strcmp($mot_01, $mot_02) : strcmp($mot_02, $mot_01); |
break; |
case self::TRIE_CASSE_INSENSIBLE : |
$resultat = ($cols[$i + 1] == SORT_DESC) ? strcasecmp($mot_01, $mot_02) : strcasecmp($mot_02, $mot_01); |
break; |
default: |
$resultat = ($cols[$i + 1] == SORT_DESC) ? strcasecmp($mot_01, $mot_02) : strcasecmp($mot_02, $mot_01); |
break; |
foreach (self::$triMultiDimension as $champ => $ordre) { |
if ($resultat == 0) { |
$mot_01 = Chaine::supprimerAccents($b[$champ]); |
$mot_02 = Chaine::supprimerAccents($a[$champ]); |
switch (self::$triType) { |
case self::TRI_NATUREL : |
$resultat = ($ordre == SORT_DESC) ? strnatcmp($mot_01, $mot_02) : strnatcmp($mot_02, $mot_01); |
break; |
case self::TRI_CASSE_SENSIBLE : |
$resultat = ($ordre == SORT_DESC) ? strcmp($mot_01, $mot_02) : strcmp($mot_02, $mot_01); |
break; |
case self::TRI_CASSE_INSENSIBLE : |
$resultat = ($ordre == SORT_DESC) ? strcasecmp($mot_01, $mot_02) : strcasecmp($mot_02, $mot_01); |
break; |
default: |
$resultat = ($ordre == SORT_DESC) ? strcasecmp($mot_01, $mot_02) : strcasecmp($mot_02, $mot_01); |
break; |
} |
|
} |
$i += 2; |
} |
return $resultat; |
} |