Witam

Mam w tablicy $table informacje o plikach w formie $table[IdPliku'][NazwaWlasciwosci], czyli tak:

  1.  
  2. // $table [ IdPliku ] [ NazwaWlasciwosci ]
  3. $table[0]['name'] = 'Code.php'; //1
  4. $table[0]['size'] = 2142; //3
  5. $table[0]['date'] = 1456589; //3
  6.  
  7. $table[1]['name'] = 'clearCode.php'; //2
  8. $table[1]['size'] = 654; //2
  9. $table[1]['date'] = 1456234; //2
  10.  
  11. $table[2]['name'] = 'index.php'; //4
  12. $table[2]['size'] = 56; //1
  13. $table[2]['date'] = 1456912; //4
  14.  
  15. $table[3]['name'] = 'functions.php'; //3
  16. $table[3]['size'] = 65142; //4
  17. $table[3]['date'] = 1456134; //1
  18.  


chce to sortowac wedlug wybranej wlasciwosci - po nazwie, po rozmiarze i po dacie
probowalem roznych funkcji, najbardziej sensowna wydaje sie ta z przykladu w manualu, ale tez nie do konca

uzyta funkcja:
  1.  
  2. function cmp($a, $b)
  3. {
  4. return strcmp($a["name"], $b["name"]);
  5. }
  6.  
  7. // $table [ IdPliku ] [ NazwaWlasciwosci ]
  8. $table[0]['name'] = 'Code.php'; //1
  9. $table[0]['size'] = 2142; //3
  10. $table[0]['date'] = 1456589; //3
  11.  
  12. $table[1]['name'] = 'clearCode.php'; //2
  13. $table[1]['size'] = 654; //2
  14. $table[1]['date'] = 1456234; //2
  15.  
  16. $table[2]['name'] = 'index.php'; //4
  17. $table[2]['size'] = 56; //1
  18. $table[2]['date'] = 1456912; //4
  19.  
  20. $table[3]['name'] = 'functions.php'; //3
  21. $table[3]['size'] = 65142; //4
  22. $table[3]['date'] = 1456134; //1
  23.  
  24.  
  25. usort($table, "cmp");
  26.  
  27. for($x=0;$table[$x]['size'];$x++)
  28. {
  29. echo $table[$x]['size'].'<br/>';
  30. }
  31.  


a tym przypadku dziala idealnie, ale jak zrobie sortowanie wedlug rozmiaru, to sortuje alfabetycznie, a nie wedlug wielkosci
wyszukalem, ze do sortowanie alfanumerycznego sluzy natsort, ale ta funkcja nie ma swojego odpowiednika do tablic wielowymiarowych

Probowalem jeszcze zrobic cos takiego:

  1. function cmp($a, $b)
  2. {
  3. if($a["size"]< $b["size"])
  4. {
  5. return -4;
  6. }
  7. }


ale to nie dziala

Jesli nie ma sposobu, to mam pomysl na napisanie wlasnego skryptu, ale strasznie skomplikowany mi sie wydaje.

EDIT:

dla
  1. function cmp($a, $b)
  2. {
  3. if($a["size"]< $b["size"])
  4. {
  5. return 5;
  6. }
  7. }

dziala, choc z opisu strcmp wynika inaczej

EDIT:
napisalem calosc

  1.  
  2. $sortType = 'size';
  3.  
  4. function cmp($a, $b)
  5. {
  6. global $sortType;
  7.  
  8. if($sortType == 'name')
  9. {
  10. return strcmp($a["name"], $b["name"]);
  11. }
  12. elseif($sortType == 'size')
  13. {
  14. if($a["size"]> $b["size"])
  15. {
  16. return 5;
  17. }
  18. }
  19. elseif($sortType == 'date')
  20. {
  21. if($a["date"]> $b["date"])
  22. {
  23. return 5;
  24. }
  25. }
  26. }
  27.  
  28. class ff
  29. {
  30.  
  31. // $table [ IdPliku ] [ NazwaWlasciwosci ]
  32. function __construct()
  33. {
  34. $this->table[0]['name'] = 'Code.php'; //1
  35. $this->table[0]['size'] = 2142; //3
  36. $this->table[0]['date'] = 1456589; //3
  37.  
  38. $this->table[1]['name'] = 'clearCode.php'; //2
  39. $this->table[1]['size'] = 654; //2
  40. $this->table[1]['date'] = 1456234; //2
  41.  
  42. $this->table[2]['name'] = 'index.php'; //4
  43. $this->table[2]['size'] = 56; //1
  44. $this->table[2]['date'] = 1456912; //4
  45.  
  46. $this->table[3]['name'] = 'functions.php'; //3
  47. $this->table[3]['size'] = 65142; //4
  48. $this->table[3]['date'] = 1456134; //1
  49. }
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56. function sort()
  57. {
  58. usort($this->table, "cmp");
  59.  
  60. for($x=0;$this->table[$x]['name'];$x++)
  61. {
  62. echo $this->table[$x]['name'].'<br/>';
  63. echo $this->table[$x]['size'].'<br/>';
  64. echo $this->table[$x]['date'].'<br/><br/>';
  65. }
  66. }
  67.  
  68. }
  69.  
  70. $f= new ff();
  71. $f->sort();
  72.  


wiec raczej temat jest do zamkniecia...