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 :

Tri de tableau (ordre naturel)


Sujet :

Langage PHP

  1. #1
    Membre à l'essai
    Profil pro
    Développeuse Web
    Inscrit en
    Octobre 2011
    Messages
    23
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeuse Web

    Informations forums :
    Inscription : Octobre 2011
    Messages : 23
    Points : 24
    Points
    24
    Par défaut Tri de tableau (ordre naturel)
    Bonjour,

    J'ai un tableau qui ressemble à ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Array ( [0] => selection0.mp3 [1] => selection1-2.mp3 [2] => selection1.mp3 [3] => selection2.mp3 [4] => selection10.mp3 )
    J'arrive à le trier de façon à ce que selection2.mp3 passe avant selection10.mp3 en utilisant natsort(). J'ai essayé avec sort() et tous les sort_flags possibles mais selection10.mp3 passe dans ces cas là toujours avant selection2.mp3.

    Seulement le problème c'est qu'ici selection1-2.mp3 devrait être APRÈS selection1.mp3 et pas avant (tel que ça s'affiche dans mon système de fichier d'ailleurs). Comment faire?! Je ne m'en sort plus!!

    À noter que ce tableau provient d'un scandir() donc si on peut faire ça directement là c'est encore mieux...

    Merci d'avance!

  2. #2
    Expert éminent
    Avatar de Benjamin Delespierre
    Profil pro
    Développeur Web
    Inscrit en
    Février 2010
    Messages
    3 929
    Détails du profil
    Informations personnelles :
    Âge : 36
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Février 2010
    Messages : 3 929
    Points : 7 762
    Points
    7 762
    Par défaut
    Si les algorithmes de tri prédéfinis par PHP ne te conviennent pas, tu peux toujours faire le tien.

    Exemple (que je te conseille d'améliorer):
    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
    for ($i = 0; $i < 50; $i++) {
    	$files[] = 'selection' . $i . '.mp3';
    	$files[] = 'selection' . $i . '-' . rand(0,10) . '.mp3';
    }
     
    // on va partir du principe que le fichier c'est toujours selection<chiffre>.mp3
    $tri = function ($a, $b) {
    	preg_match('~(\d+)-?(\d+)?~', $a, $ma);
    	preg_match('~(\d+)-?(\d+)?~', $b, $mb);
    	$a1 = (int)$ma[1]; $a2 = isset($ma[2]) ? (int)$ma[2] : 1;
    	$b1 = (int)$mb[1]; $b2 = isset($mb[2]) ? (int)$mb[2] : 1;
    	if ($a1 > $b1 ||  ($a1 == $b1 && $a2 > $b2)) return 1;
    	if ($a1 == $b1 && $a2 == $b2) return 0;
    	return -1;
    };
     
    shuffle($files);
    var_dump('Before', $files);
    usort($files, $tri);
    var_dump('after', $files);

  3. #3
    Membre éprouvé Avatar de patrickbaras
    Homme Profil pro
    Informaticien (à sa mémère).
    Inscrit en
    Septembre 2010
    Messages
    525
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 60
    Localisation : Belgique

    Informations professionnelles :
    Activité : Informaticien (à sa mémère).
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Septembre 2010
    Messages : 525
    Points : 1 103
    Points
    1 103
    Par défaut
    un autre exemple de tri personnalisé
    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
    <?php
    // Fonction de comparaison
    function fn_tri_mp3($a, $b) {
    	$a=str_replace(".mp3","",$a);
    	$b=str_replace(".mp3","",$b);
     
    	$a=str_replace("-"," ",$a);
    	$b=str_replace("-"," ",$b);
     
        if ($a == $b) {
            return 0;
        }
        return ($a < $b) ? -1 : 1;
    }
     
    $array	[0] = "selection0.mp3";
    $array	[1] = "selection1-2.mp3";
    $array	[2] = "selection1.mp3";
    $array	[3] = "selection2.mp3";
    $array	[4] = "selection10.mp3";
     
    print_r($array);
    echo "<br/>";
    // Trie et affiche le tableau résultant
    uasort($array, 'fn_tri_mp3');
    print_r($array);
    ?>
    resultat:
    Array ( [0] => selection0.mp3 [2] => selection1.mp3 [1] => selection1-2.mp3 [4] => selection10.mp3 [3] => selection2.mp3 )

  4. #4
    Expert éminent
    Avatar de Benjamin Delespierre
    Profil pro
    Développeur Web
    Inscrit en
    Février 2010
    Messages
    3 929
    Détails du profil
    Informations personnelles :
    Âge : 36
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Février 2010
    Messages : 3 929
    Points : 7 762
    Points
    7 762
    Par défaut
    @patrickbaras

    C'est de la magie noire, ton algo repose sur le cast implicite string > integer.
    Du coup le résultat produit est:
    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
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    array
      0 => string 'selection0.mp3' (length=14)
      1 => string 'selection0-0.mp3' (length=16)
      2 => string 'selection1.mp3' (length=14)
      3 => string 'selection1-4.mp3' (length=16)
      4 => string 'selection10.mp3' (length=15)
      5 => string 'selection10-10.mp3' (length=18)
      6 => string 'selection11.mp3' (length=15)
      7 => string 'selection11-1.mp3' (length=17)
      8 => string 'selection12.mp3' (length=15)
      9 => string 'selection12-6.mp3' (length=17)
      10 => string 'selection13.mp3' (length=15)
      11 => string 'selection13-2.mp3' (length=17)
      12 => string 'selection14.mp3' (length=15)
      13 => string 'selection14-5.mp3' (length=17)
      14 => string 'selection15.mp3' (length=15)
      15 => string 'selection15-0.mp3' (length=17)
      16 => string 'selection16.mp3' (length=15)
      17 => string 'selection16-0.mp3' (length=17)
      18 => string 'selection17.mp3' (length=15)
      19 => string 'selection17-10.mp3' (length=18)
      20 => string 'selection18.mp3' (length=15)
      21 => string 'selection18-2.mp3' (length=17)
      22 => string 'selection19.mp3' (length=15)
      23 => string 'selection19-0.mp3' (length=17)
      24 => string 'selection2.mp3' (length=14)
      25 => string 'selection2-2.mp3' (length=16)
      26 => string 'selection20.mp3' (length=15)
      27 => string 'selection20-4.mp3' (length=17)
      28 => string 'selection21.mp3' (length=15)
      29 => string 'selection21-6.mp3' (length=17)
      30 => string 'selection22.mp3' (length=15)
      31 => string 'selection22-8.mp3' (length=17)
      32 => string 'selection23.mp3' (length=15)
      33 => string 'selection23-1.mp3' (length=17)
      34 => string 'selection24.mp3' (length=15)
      35 => string 'selection24-1.mp3' (length=17)
      36 => string 'selection25.mp3' (length=15)
      37 => string 'selection25-10.mp3' (length=18)
      38 => string 'selection26.mp3' (length=15)
      39 => string 'selection26-10.mp3' (length=18)
      40 => string 'selection27.mp3' (length=15)
      41 => string 'selection27-0.mp3' (length=17)
      42 => string 'selection28.mp3' (length=15)
      43 => string 'selection28-9.mp3' (length=17)
      44 => string 'selection29.mp3' (length=15)
      45 => string 'selection29-4.mp3' (length=17)
      46 => string 'selection3.mp3' (length=14)
      47 => string 'selection3-5.mp3' (length=16)
      48 => string 'selection30.mp3' (length=15)
      49 => string 'selection30-3.mp3' (length=17)
      50 => string 'selection31.mp3' (length=15)
      51 => string 'selection31-7.mp3' (length=17)
      52 => string 'selection32.mp3' (length=15)
      53 => string 'selection32-7.mp3' (length=17)
      54 => string 'selection33.mp3' (length=15)
      55 => string 'selection33-8.mp3' (length=17)
      56 => string 'selection34.mp3' (length=15)
      57 => string 'selection34-8.mp3' (length=17)
      58 => string 'selection35.mp3' (length=15)
      59 => string 'selection35-8.mp3' (length=17)
      60 => string 'selection36.mp3' (length=15)
      61 => string 'selection36-5.mp3' (length=17)
      62 => string 'selection37.mp3' (length=15)
      63 => string 'selection37-4.mp3' (length=17)
      64 => string 'selection38.mp3' (length=15)
      65 => string 'selection38-9.mp3' (length=17)
      66 => string 'selection39.mp3' (length=15)
      67 => string 'selection39-3.mp3' (length=17)
      68 => string 'selection4.mp3' (length=14)
      69 => string 'selection4-6.mp3' (length=16)
      70 => string 'selection40.mp3' (length=15)
      71 => string 'selection40-4.mp3' (length=17)
      72 => string 'selection41.mp3' (length=15)
      73 => string 'selection41-2.mp3' (length=17)
      74 => string 'selection42.mp3' (length=15)
      75 => string 'selection42-5.mp3' (length=17)
      76 => string 'selection43.mp3' (length=15)
      77 => string 'selection43-10.mp3' (length=18)
      78 => string 'selection44.mp3' (length=15)
      79 => string 'selection44-2.mp3' (length=17)
      80 => string 'selection45.mp3' (length=15)
      81 => string 'selection45-4.mp3' (length=17)
      82 => string 'selection46.mp3' (length=15)
      83 => string 'selection46-1.mp3' (length=17)
      84 => string 'selection47.mp3' (length=15)
      85 => string 'selection47-9.mp3' (length=17)
      86 => string 'selection48.mp3' (length=15)
      87 => string 'selection48-1.mp3' (length=17)
      88 => string 'selection49.mp3' (length=15)
      89 => string 'selection49-10.mp3' (length=18)
      90 => string 'selection5.mp3' (length=14)
      91 => string 'selection5-6.mp3' (length=16)
      92 => string 'selection6.mp3' (length=14)
      93 => string 'selection6-7.mp3' (length=16)
      94 => string 'selection7.mp3' (length=14)
      95 => string 'selection7-6.mp3' (length=16)
      96 => string 'selection8.mp3' (length=14)
      97 => string 'selection8-6.mp3' (length=16)
      98 => string 'selection9.mp3' (length=14)
      99 => string 'selection9-0.mp3' (length=16)
    Remarque, si c'est ce qu'on veut pourquoi pas après tout. Personnellement je n'ai jamais apprécié avoir 10 avant 2 mais bon.

  5. #5
    Membre éprouvé Avatar de patrickbaras
    Homme Profil pro
    Informaticien (à sa mémère).
    Inscrit en
    Septembre 2010
    Messages
    525
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 60
    Localisation : Belgique

    Informations professionnelles :
    Activité : Informaticien (à sa mémère).
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Septembre 2010
    Messages : 525
    Points : 1 103
    Points
    1 103
    Par défaut
    si on veux plus "conventionel" (lol)
    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
    <?php
    // Fonction de comparaison
    function fn_tri_mp3($a, $b) {
    	$a=str_replace(".mp3","",$a);
    	$b=str_replace(".mp3","",$b);
     
    	$a=str_replace("selection","",$a);
    	$b=str_replace("selection","",$b);
     
     
    	$a=str_replace("-",".",$a);
    	$b=str_replace("-",".",$b);
     
        if ($a == $b) {
            return 0;
        }
        return ($a < $b) ? -1 : 1;
    }
    $array	[0] = "selection0.mp3";
    $array	[1] = "selection1-2.mp3";
    $array	[2] = "selection1.mp3";
    $array	[3] = "selection2.mp3";
    $array	[4] = "selection10.mp3";
    print_r($array);
    echo "<br/>";
    // Trie et affiche le tableau résultant
    uasort($array, 'fn_tri_mp3');
    print_r($array);
    ?>
    resultat:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Array ( [0] => selection0.mp3 [2] => selection1.mp3 [1] => selection1-2.mp3 [3] => selection2.mp3 [4] => selection10.mp3 )
    comme ca le 2 viens avant le 10 (bola-bola-zola : expression de magie noir)
    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
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    Array
    (
        [0] => selection0.mp3
        [1] => selection0-1.mp3
        [2] => selection1.mp3
        [3] => selection1-6.mp3
        [4] => selection2.mp3
        [5] => selection2-8.mp3
        [6] => selection3.mp3
        [7] => selection3-8.mp3
        [8] => selection4.mp3
        [9] => selection4-3.mp3
        [11] => selection5-0.mp3
        [10] => selection5.mp3
        [12] => selection6.mp3
        [13] => selection6-5.mp3
        [14] => selection7.mp3
        [15] => selection7-1.mp3
        [17] => selection8-0.mp3
        [16] => selection8.mp3
        [19] => selection9-0.mp3
        [18] => selection9.mp3
        [20] => selection10.mp3
        [21] => selection10-3.mp3
        [22] => selection11.mp3
        [23] => selection11-6.mp3
        [24] => selection12.mp3
        [25] => selection12-9.mp3
        [27] => selection13-0.mp3
        [26] => selection13.mp3
        [28] => selection14.mp3
        [29] => selection14-4.mp3
        [30] => selection15.mp3
        [31] => selection15-10.mp3
        [32] => selection16.mp3
        [33] => selection16-3.mp3
        [34] => selection17.mp3
        [35] => selection17-3.mp3
        [36] => selection18.mp3
        [37] => selection18-7.mp3
        [38] => selection19.mp3
        [39] => selection19-7.mp3
        [40] => selection20.mp3
        [41] => selection20-8.mp3
        [42] => selection21.mp3
        [43] => selection21-6.mp3
        [44] => selection22.mp3
        [45] => selection22-6.mp3
        [46] => selection23.mp3
        [47] => selection23-8.mp3
        [48] => selection24.mp3
        [49] => selection24-5.mp3
        [50] => selection25.mp3
        [51] => selection25-4.mp3
        [52] => selection26.mp3
        [53] => selection26-7.mp3
        [54] => selection27.mp3
        [55] => selection27-10.mp3
        [56] => selection28.mp3
        [57] => selection28-7.mp3
        [58] => selection29.mp3
        [59] => selection29-8.mp3
        [60] => selection30.mp3
        [61] => selection30-4.mp3
        [62] => selection31.mp3
        [63] => selection31-9.mp3
        [64] => selection32.mp3
        [65] => selection32-4.mp3
        [66] => selection33.mp3
        [67] => selection33-1.mp3
        [68] => selection34.mp3
        [69] => selection34-6.mp3
        [70] => selection35.mp3
        [71] => selection35-8.mp3
        [72] => selection36.mp3
        [73] => selection36-2.mp3
        [74] => selection37.mp3
        [75] => selection37-1.mp3
        [76] => selection38.mp3
        [77] => selection38-9.mp3
        [78] => selection39.mp3
        [79] => selection39-2.mp3
        [80] => selection40.mp3
        [81] => selection40-1.mp3
        [82] => selection41.mp3
        [83] => selection41-1.mp3
        [84] => selection42.mp3
        [85] => selection42-8.mp3
        [87] => selection43-0.mp3
        [86] => selection43.mp3
        [88] => selection44.mp3
        [89] => selection44-2.mp3
        [90] => selection45.mp3
        [91] => selection45-1.mp3
        [93] => selection46-0.mp3
        [92] => selection46.mp3
        [94] => selection47.mp3
        [95] => selection47-5.mp3
        [96] => selection48.mp3
        [97] => selection48-4.mp3
        [98] => selection49.mp3
        [99] => selection49-8.mp3
    )

  6. #6
    Expert éminent
    Avatar de Benjamin Delespierre
    Profil pro
    Développeur Web
    Inscrit en
    Février 2010
    Messages
    3 929
    Détails du profil
    Informations personnelles :
    Âge : 36
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Février 2010
    Messages : 3 929
    Points : 7 762
    Points
    7 762
    Par défaut
    str_replace accepte des tableaux
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    $s = array('.mp3', 'selection', '-');
    $r = array('', '', '.');
    $a = str_replace($s, $r, $a);
    $b = str_replace($s, $r, $b);

  7. #7
    Membre éprouvé Avatar de patrickbaras
    Homme Profil pro
    Informaticien (à sa mémère).
    Inscrit en
    Septembre 2010
    Messages
    525
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 60
    Localisation : Belgique

    Informations professionnelles :
    Activité : Informaticien (à sa mémère).
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Septembre 2010
    Messages : 525
    Points : 1 103
    Points
    1 103
    Par défaut
    sans tenir compte de selection et .mp3
    évidement ne tient compte que de la premier partie 00-00 (avant le .)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <?php
    // Fonction de comparaison
    function fn_tri_mp3($a, $b) {
    	preg_match("/[0-9]+-?[0-9]*(?=\..*$)/",$a,$matches);
    	$a=str_replace("-",".",$matches[0]);
    	preg_match("/[0-9]+-?[0-9]*(?=\..*$)/",$b,$matches);
    	$b=str_replace("-",".",$matches[0]);
        if ($a == $b) {
            return 0;
        }
        return ($a < $b) ? -1 : 1;
    }
    ?>

  8. #8
    Membre à l'essai
    Profil pro
    Développeuse Web
    Inscrit en
    Octobre 2011
    Messages
    23
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeuse Web

    Informations forums :
    Inscription : Octobre 2011
    Messages : 23
    Points : 24
    Points
    24
    Par défaut
    Merci beaucoup pour toutes vos réponses!
    Bon là je suis face à de la magie noire héhé... Ou a un féroce manque de connaissances ce qui semble plus probable

    Je finis donc avec cette fonction:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    function fn_tri_mp3($a, $b) {
    	$s = array('.mp3', 'selection', '-');
    	$r = array('', '', '.');
    	$a = str_replace($s, $r, $a);
    	$b = str_replace($s, $r, $b);
     
    	if ($a == $b) {
    		return 0;
    	}
     
    	return ($a < $b) ? -1 : 1;
    }
    Et là, tindin... Quand je l'applique au tableau "exemple" tout va bien :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    $array	[0] = "selection0.mp3";
    $array	[1] = "selection1-2.mp3";
    $array	[2] = "selection1.mp3";
    $array	[3] = "selection2.mp3";
    $array	[4] = "selection10.mp3";
     
    uasort($array, 'fn_tri_mp3');
    print_r($array);
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Array ( [0] => selection0.mp3 [2] => selection1.mp3 [1] => selection1-2.mp3 [3] => selection2.mp3 [4] => selection10.mp3 )
    Mais quand je l'applique à MON tableau, le selection10 repasse avant le selection2 ??? Peut-être à cause du chemin complet qui est avant?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    uasort($mp3_path, 'fn_tri_mp3');
    print_r($mp3_path);
    résultat (j'ai retiré la fin pour plus de lisibilité, j'ai 13 entrées) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Array ( [0] => music/Cant Even Compare/selection0.mp3 [2] => music/Cant Even Compare/selection1.mp3 [1] => music/Cant Even Compare/selection1-2.mp3 [11] => music/Cant Even Compare/selection10.mp3)

    Mais quel est ce mystère?!? Auriez-vous une idée? En attendant je jète un oeil à la dernière version de fn_tri_mp3($a, $b) proposée par Patrick et vous en donne des nouvelles (mais mince ça reste bizarre cette histoire! )

  9. #9
    Membre à l'essai
    Profil pro
    Développeuse Web
    Inscrit en
    Octobre 2011
    Messages
    23
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeuse Web

    Informations forums :
    Inscription : Octobre 2011
    Messages : 23
    Points : 24
    Points
    24
    Par défaut
    Re!
    Bon avec la dernière c'est mieux ET pire. Si on part du principe que le nom sera toujours selection**.mp3 c'est mieux. Sauf que ce n'est pas toujours le cas. Ici, tous les noms suivis d'autre chose ont été classé en premier. Après ça va bien. tableau complet, ça va aider:

    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
     
    Array ( [4] => music/Cant Even Compare/selection3-effiler.mp3 
    [10] => music/Cant Even Compare/selection9-ceschoses.mp3 
    [9] => music/Cant Even Compare/selection8-TheBest.mp3 
    [3] => music/Cant Even Compare/selection2-Liar.mp3 
    [0] => music/Cant Even Compare/selection0.mp3 
    [2] => music/Cant Even Compare/selection1.mp3 
    [1] => music/Cant Even Compare/selection1-2.mp3 
    [5] => music/Cant Even Compare/selection4.mp3 
    [6] => music/Cant Even Compare/selection5.mp3 
    [7] => music/Cant Even Compare/selection6.mp3 
    [8] => music/Cant Even Compare/selection7.mp3 
    [11] => music/Cant Even Compare/selection10.mp3 
    [12] => music/Cant Even Compare/selection11.mp3 
    [13] => music/Cant Even Compare/selection12.mp3 )
    Bien sur je n'aurais pas la main sur le nommage des fichiers et ils peuvent être à peu près n'importe quoi (mais ressemblent en général à ça). Sachant qu'avec _ au lieu de - ça se tri bien j'avais pensé à faire un str_replace pour le tri mais si j'ai d'autres _ dans le noms de fichiers ils seront également remplacés pour remettre comme il faut...

    J'ai l'impression que les fonctions passées en paramètres d'un uasort() servent justement à modifier les valeurs du tableau le temps du tri seulement? C'est bien ça? J'ai du mal à décrypter cette fonction je dois dire, surtout le return...

    Merci!

  10. #10
    Membre à l'essai
    Profil pro
    Développeuse Web
    Inscrit en
    Octobre 2011
    Messages
    23
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeuse Web

    Informations forums :
    Inscription : Octobre 2011
    Messages : 23
    Points : 24
    Points
    24
    Par défaut
    Re bonsoir!
    C'est bon, après avoir réussi à comprendre comment ça marchait, voici la fonction finale adaptée (et simplifiée du coup. Pourquoi le sous-masque à la fin de l'autre version? Pas compris) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    function fn_tri_mp3($a, $b) {
    	preg_match("/[0-9]+(\-[0-9]+)?/",$a,$matches);
    	$a=str_replace("-",".",$matches[0]);
    	preg_match("/[0-9]+(\-[0-9]+)?/",$b,$matches);
    	$b=str_replace("-",".",$matches[0]);	
     
        if ($a == $b) {
            return 0;
        }
        return ($a < $b) ? -1 : 1;
    }
    Et voila ça fonctionne à merveille
    Plus qu'à renuméroter les cléfs pour mes besoins donc usort() au lieu de uasort().
    Merci encore!

  11. #11
    Membre éprouvé Avatar de patrickbaras
    Homme Profil pro
    Informaticien (à sa mémère).
    Inscrit en
    Septembre 2010
    Messages
    525
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 60
    Localisation : Belgique

    Informations professionnelles :
    Activité : Informaticien (à sa mémère).
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Septembre 2010
    Messages : 525
    Points : 1 103
    Points
    1 103
    Par défaut
    (?=\..*$)
    c'etait pour prendre unique les chiffres précédant .****** en fin de chaine (mais sans prendre ce . et le reste jusqu'a la fin)

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [XL-2007] Tri étendu par ordre alphabétique sur un tableau aux dimensions dynamiques
    Par atk_49 dans le forum Macros et VBA Excel
    Réponses: 5
    Dernier message: 17/03/2014, 15h03
  2. [2K5] Tri par ordre naturel
    Par mioux dans le forum MS SQL Server
    Réponses: 0
    Dernier message: 04/03/2008, 11h56
  3. [débutant] tri vecteur string ordre alphabétique
    Par Tom Sawyer dans le forum SL & STL
    Réponses: 2
    Dernier message: 26/07/2004, 17h40
  4. [langage] tri dans tableau de hachage
    Par mimilou dans le forum Langage
    Réponses: 2
    Dernier message: 10/03/2004, 16h10
  5. tri avec l'ordre UPDATE et incrementation d'une colonne
    Par Staron dans le forum Langage SQL
    Réponses: 3
    Dernier message: 17/02/2004, 08h48

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