Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Sortowanie tablicy z obiektami, usort nie sortuje "do końca"
Blackhole
post
Post #1





Grupa: Zarejestrowani
Postów: 283
Pomógł: 1
Dołączył: 15.11.2004
Skąd: Mikołów

Ostrzeżenie: (0%)
-----


Hej.
Mam tablicę $pla zawierająca obiekty z atrybutami: dz, h, m, gru, nau, prz, sal.
Chcę sobie posortować tę tablicę rosnąco po: dz, h, m. Mam więc do tego funkcję:
  1. function sort_pla($a, $b) {
  2. if ($a->nau < $b->nau or
  3. $a->dz < $b->dz or
  4. $a->h < $b->h or
  5. $a->m < $b->m)
  6. return -1;
  7. else return 1;
  8. }
Niestety wykonanie:
  1. usort($pla, 'sort_pla');
pozostawia $pla w postaci:
  1. array(10) {
  2. [0]=>
  3. object(plan)#98 (7) {
  4. ["dz"]=> int(0)
  5. ["h"]=> int(7)
  6. ["m"]=> int(30)
  7. ["gru"]=> string(1) "1"
  8. ["prz"]=> string(2) "18"
  9. ["nau"]=> string(1) "9"
  10. ["sal"]=> string(2) "16"
  11. }
  12. [1]=>
  13. object(plan)#99 (7) {
  14. ["dz"]=> int(0)
  15. ["h"]=> int(7)
  16. ["m"]=> int(35)
  17. ["gru"]=> string(1) "1"
  18. ["prz"]=> string(2) "18"
  19. ["nau"]=> string(1) "9"
  20. ["sal"]=> string(2) "16"
  21. }
  22. [2]=>
  23. object(plan)#77 (7) {
  24. ["dz"]=> int(1)
  25. ["h"]=> int(7)
  26. ["m"]=> int(20)
  27. ["gru"]=> string(0) ""
  28. ["prz"]=> string(2) "17"
  29. ["nau"]=> string(1) "1"
  30. ["sal"]=> string(1) "5"
  31. }
  32. [3]=>
  33. object(plan)#71 (7) {
  34. ["dz"]=> int(0)
  35. ["h"]=> int(7)
  36. ["m"]=> int(40)
  37. ["gru"]=> string(1) "1"
  38. ["prz"]=> string(2) "18"
  39. ["nau"]=> string(1) "9"
  40. ["sal"]=> string(2) "16"
  41. }
  42. [4]=>
  43. object(plan)#78 (7) {
  44. ["dz"]=> int(1)
  45. ["h"]=> int(7)
  46. ["m"]=> int(25)
  47. ["gru"]=> string(0) ""
  48. ["prz"]=> string(2) "17"
  49. ["nau"]=> string(1) "1"
  50. ["sal"]=> string(1) "5"
  51. }
  52. [5]=>
  53. object(plan)#72 (7) {
  54. ["dz"]=> int(0)
  55. ["h"]=> int(7)
  56. ["m"]=> int(45)
  57. ["gru"]=> string(1) "1"
  58. ["prz"]=> string(2) "18"
  59. ["nau"]=> string(1) "9"
  60. ["sal"]=> string(2) "16"
  61. }
  62. [6]=>
  63. object(plan)#76 (7) {
  64. ["dz"]=> int(1)
  65. ["h"]=> int(7)
  66. ["m"]=> int(15)
  67. ["gru"]=> string(0) ""
  68. ["prz"]=> string(2) "17"
  69. ["nau"]=> string(1) "1"
  70. ["sal"]=> string(1) "5"
  71. }
  72. [7]=>
  73. object(plan)#73 (7) {
  74. ["dz"]=> int(0)
  75. ["h"]=> int(7)
  76. ["m"]=> int(50)
  77. ["gru"]=> string(1) "1"
  78. ["prz"]=> string(2) "18"
  79. ["nau"]=> string(1) "9"
  80. ["sal"]=> string(2) "16"
  81. }
  82. [8]=>
  83. object(plan)#74 (7) {
  84. ["dz"]=> int(0)
  85. ["h"]=> int(7)
  86. ["m"]=> int(55)
  87. ["gru"]=> string(1) "1"
  88. ["prz"]=> string(2) "18"
  89. ["nau"]=> string(1) "9"
  90. ["sal"]=> string(2) "16"
  91. }
  92. [9]=>
  93. object(plan)#75 (7) {
  94. ["dz"]=> int(1)
  95. ["h"]=> int(7)
  96. ["m"]=> int(10)
  97. ["gru"]=> string(0) ""
  98. ["prz"]=> string(2) "17"
  99. ["nau"]=> string(1) "1"
  100. ["sal"]=> string(1) "5"
  101. }
  102. }
Co zepsułem? A może czegoś nie rozumiem?

Ten post edytował Blackhole 20.07.2014, 11:10:23
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 4)
SmokAnalog
post
Post #2





Grupa: Zarejestrowani
Postów: 1 707
Pomógł: 266
Dołączył: 3.07.2012
Skąd: Poznań

Ostrzeżenie: (0%)
-----


Spróbuj coś takiego:

  1. function sort_pla($a, $b) {
  2. return ($a->nau - $b->nau) | ($a->dz - $b->dz) | ($a->h - $b->h) | ($a->m - $b->m);
  3. }


Albo trochę bardziej czytelnie:

  1. function sort_pla($a, $b) {
  2. $fields = array('nau', 'dz', 'h', 'm');
  3.  
  4. foreach ($fields as $field) {
  5. $diff = $a->$field - $b->$field;
  6. if ($diff !== 0) return $diff;
  7. }
  8.  
  9. return 0;
  10. }


Ten post edytował SmokAnalog 20.07.2014, 11:40:23
Go to the top of the page
+Quote Post
Blackhole
post
Post #3





Grupa: Zarejestrowani
Postów: 283
Pomógł: 1
Dołączył: 15.11.2004
Skąd: Mikołów

Ostrzeżenie: (0%)
-----


Nie pomogło. Nadal są sytuacje, że w tablicy (przykładowo) na pozycji x1 jest obiekt z nau=n1, na x2 z nau=n2, a na x3 znów z nau=n1 (IMG:style_emoticons/default/ohno-smiley.gif) (to dotyczy tej mniej czytelnej Twojej sugestii)

Ten post edytował Blackhole 20.07.2014, 11:49:39
Go to the top of the page
+Quote Post
SmokAnalog
post
Post #4





Grupa: Zarejestrowani
Postów: 1 707
Pomógł: 266
Dołączył: 3.07.2012
Skąd: Poznań

Ostrzeżenie: (0%)
-----


Zrobiłem test i ten drugi kod działa (IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post
Blackhole
post
Post #5





Grupa: Zarejestrowani
Postów: 283
Pomógł: 1
Dołączył: 15.11.2004
Skąd: Mikołów

Ostrzeżenie: (0%)
-----


Dzięki! Mnie też zadziałało (IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 12.10.2025 - 08:21