Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Sortowanie
Tomplus
post 24.07.2008, 23:58:59
Post #1





Grupa: Zarejestrowani
Postów: 1 874
Pomógł: 230
Dołączył: 20.03.2005
Skąd: Będzin

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


Witam,
Mam mały problemik i prosiłbym o pomoc w jego rozwiązaniu:
o to kod problemu:
  1. <?php
  2. $a[0]['name'] = 'Alfa';
  3. $a[1]['name'] = 'Beta';
  4. $a[2]['name'] = 'Gamma';
  5. $a[3]['name'] = 'Delta';
  6. $a[4]['name'] = 'Omega';
  7. $a[5]['name'] = 'Pi';
  8. $a[6]['name'] = 'Tau';
  9. $a[7]['name'] = 'Epsilon';
  10. $a[8]['name'] = 'Sigma';
  11. $a[9]['name'] = 'Lambda';
  12.  
  13. $a[0]['num1'] = rand(0,9);
  14. $a[1]['num1'] = rand(0,9);
  15. $a[2]['num1'] = rand(0,9);
  16. $a[3]['num1'] = rand(0,9);
  17. $a[4]['num1'] = rand(0,9);
  18. $a[5]['num1'] = rand(0,9);
  19. $a[6]['num1'] = rand(0,9);
  20. $a[7]['num1'] = rand(0,9);
  21. $a[8]['num1'] = rand(0,9);
  22. $a[9]['num1'] = rand(0,9);
  23.  
  24.  
  25.  
  26. $a[0]['num2'] = rand(0,9);
  27. $a[1]['num2'] = rand(0,9);
  28. $a[2]['num2'] = rand(0,9);
  29. $a[3]['num2'] = rand(0,9);
  30. $a[4]['num2'] = rand(0,9);
  31. $a[5]['num2'] = rand(0,9);
  32. $a[6]['num2'] = rand(0,9);
  33. $a[7]['num2'] = rand(0,9);
  34. $a[8]['num2'] = rand(0,9);
  35. $a[9]['num2'] = rand(0,9);
  36.  
  37. $a[0]['num3'] = $a[0]['num1'] + $a[0]['num2'];
  38. $a[1]['num3'] = $a[1]['num1'] + $a[1]['num2'];
  39. $a[2]['num3'] = $a[2]['num1'] + $a[2]['num2'];
  40. $a[3]['num3'] = $a[3]['num1'] + $a[3]['num2'];
  41. $a[4]['num3'] = $a[4]['num1'] + $a[4]['num2'];
  42. $a[5]['num3'] = $a[5]['num1'] + $a[5]['num2'];
  43. $a[6]['num3'] = $a[6]['num1'] + $a[6]['num2'];
  44. $a[7]['num3'] = $a[7]['num1'] + $a[7]['num2'];
  45. $a[8]['num3'] = $a[8]['num1'] + $a[8]['num2'];
  46. $a[9]['num3'] = $a[9]['num1'] + $a[9]['num2'];
  47.  
  48. /*
  49. lp name  num1 num2
  50. 1. Gamma  3  6
  51. 2. Omega  3  4
  52. 3. Alfa 3  3
  53. 4. Delta  2  4
  54. 5. Beta 1  1
  55. */
  56.  
  57. $p=0;
  58. $sortuj[$p]['name'] = "num3";
  59. $sortuj[$p]['sort'] = "DESC";
  60. $sortuj[$p++]['case'] = TRUE;
  61. $sortuj[$p]['name'] = "num2";
  62. $sortuj[$p]['sort'] = "DESC";
  63. $sortuj[$p++]['case'] = TRUE;
  64. $sortuj[$p]['name'] = "num1";
  65. $sortuj[$p]['sort'] = "DESC";
  66. $sortuj[$p++]['case'] = TRUE;
  67. $sortuj[$p]['name'] = "name";
  68. $sortuj[$p]['sort'] = "ASC";
  69. $sortuj[$p++]['case'] = FALSE;
  70.  
  71.  
  72. function sortx(&$tablica, $sort = array())
  73. {
  74.  $function = '';
  75.  while (list($key) = each($sort))
  76.  {
  77.  if (isset($sort[$key]['case']) && ($sort[$key]['case'] == TRUE)) $function .= 'if (strtolower($a["'.$sort[$key]['name'].'"])<>strtolower($b["'.$sort[$key]['name'].'"])) { return (strtolower($a["'.$sort[$key]['name'].'"]) ';
  78.  else $function .= 'if ($a["'.$sort[$key]['name'].'"]<>$b["'.$sort[$key]['name'].'"]) { return ($a["'.$sort[$key]['name'].'"] ';
  79.  
  80.  if (isset($sort[$key]['sort']) && ($sort[$key]['sort'] == "DESC")) $function .= '<';
  81.  else $function .= '>';
  82.  
  83.  if (isset($sort[$key]['case']) && ($sort[$key]['case'] == TRUE)) $function .= ' strtolower($b["'.$sort[$key]['name'].'"])) ? 1 : -1; } else';
  84.  else $function .= ' $b["'.$sort[$key]['name'].'"]) ? 1 : -1; } else';
  85.  }
  86.  $function .= ' { return 0; }';  
  87.  usort($tablica, create_function('$a, $b', $function));
  88. }
  89.  
  90. $p=0;
  91. $str2 = $str3 = '';
  92. $str1 = '<table><tr><th>lp</th><th>Name</th>
  93. <th>Num1</th><th>Num2</th><th>Num3</th>';
  94. sortx($a, $sortuj);
  95. while (list($key, $value) = each($a))
  96. {
  97.  $p++;
  98.  if ($p<=5) $str2.= "<tr style='background: #fcc;'><td>{$p}</td><td><strong>{$value['name']}</strong></td>
  99.  <td>{$value['num1']}</td><td>{$value['num2']}</td><td>{$value['num3']}</td>";
  100.  else $str3 .= "<tr style='background: #ccc;'><td>{$p}</td><td><strong>{$value['name']}</strong></td>
  101.  <td>{$value['num1']}</td><td></td><td>{$value['num1']}</td>";
  102.  
  103.  
  104. }
  105.  
  106. $str4 = "</td></tr></foot></table>";
  107. echo $str1.$str2.$str3.$str4;
  108.  
  109. ?>


Działający kod znajduje się tutaj: http://www.swos.pl/temp/sortowanie.php


Wizja moja jest następująca:
Cała lista jest sortowane wg. wartości NUM3, ale tylko dla pierwszych 5 NAJWYŻSZYCH wartości, a potem jest sortowanie wg. NUM1.

W tej chwili wyniki są w połowie spaczone.
Gdyż sortuje wg. NUM3, ale sortowanie wg. NUM1 nie działa, a nie można zrobić żeby sortowało najpierw wg. NUM1, bo wteyd zostaną spaczone wyniki NUM3(suma NUM1 i NUM2)

Jakie będzie najlepsze rozwiązanie ? Moje pomysły zawodzą.

Ten post edytował Tomplus 28.07.2008, 13:08:01
Go to the top of the page
+Quote Post
Zyx
post 25.07.2008, 09:38:25
Post #2





Grupa: Zarejestrowani
Postów: 952
Pomógł: 154
Dołączył: 20.01.2007
Skąd: /dev/oracle

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


1. Odnajdujesz 5 najwyższych wartości i referencje (albo indeksy) do nich wkładasz do tablicy 1.
2. Pozostałe wartości (albo referencje/indeksy) dajesz do tablicy 2.
3. Sortujesz tablicę 1 według NUM3
4. Sortujesz tablicę 2 według NUM1
5. Scalasz obie tablice z powrotem.

Możesz to zrobić nawet funkcjami PHP, tylko musisz sobie własną funkcję porównującą napisać.

Mam nadzieję, że dobrze zrozumiałem Twoje intencje smile.gif.


--------------------
Specjalista ds. głupich i beznadziejnych, Zyx
Nowości wydawnicze: Open Power Collector 3.0.1.0 | Open Power Autoloader 3.0.3.0
Go to the top of the page
+Quote Post
Tomplus
post 28.07.2008, 13:20:00
Post #3





Grupa: Zarejestrowani
Postów: 1 874
Pomógł: 230
Dołączył: 20.03.2005
Skąd: Będzin

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


DObrze zrozumiałeś, ale się zastanawiałem nad ulepszeniem sortowania przed while(), Czy da się coś takiego zrobić ?

Dodałem do całego skryptu:
  1. <?php
  2. $a[1]['i'] = 1;
  3. $a[2]['i'] = 2;
  4. $a[3]['i'] = 3;
  5. $a[4]['i'] = 4;
  6. $a[5]['i'] = 5;
  7. $a[6]['i'] = 6;
  8. $a[7]['i'] = 7;
  9. $a[8]['i'] = 8;
  10. $a[9]['i'] = 9;
  11. ?>



oraz zmodyfikowałem pętle do takich postaci:

  1. <?php
  2.  $p=0;
  3. $str2 = $str3 = '';
  4. $str1 = '<table><tr><th>lp</th><th>Name</th>
  5. <th>Num1</th><th>Num2</th><th>Num3</th>';
  6. sortx($a, $sortuj2);
  7. while (list($key, $value) = each($a))
  8. {
  9.  $p++;
  10.  if ($p>5) $str3 .= "<tr style='background: #ccc;'><td>{$p}</td><td><strong>{$value['name']}</strong></td>
  11. <td>{$value['num1']}</td><td></td><td>{$value['num1']}</td>";
  12. }
  13.  
  14. array_splice ($a, 5);
  15.  
  16. $p=0;
  17. sortx($a, $sortuj);
  18. while (list($key, $value) = each($a))
  19. {
  20.  $p++;
  21.  $str2.= "<tr style='background: #fcc;'><td>{$p}</td><td><strong>{$value['name']}</strong></td>
  22.  <td>{$value['num1']}</td><td>{$value['num2']}</td><td>{$value['num3']}</td>";
  23.  
  24.  
  25. }
  26.  
  27. $str4 = "</table>";
  28. echo $str1.$str2.$str3.$str4;
  29. ?>



[edit]
Usunąłem moje stare zapytanie, bo jest już nie aktualne, a nikt nie odpowiedział.

Problem rozwiązany w sposób prosty. Między dwoma while() umieściłem array_splice ($a, 5);, dzięki tej funkcji ucina tablicę do tylko 5 wpisów i drugie sortowanie może tylko posortować tą pozostałą część wpisów.

Inaczej mówiąc. O to chodziło.

Ten post edytował Tomplus 29.07.2008, 11:29:36
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 28.06.2025 - 05:38