codeigniter bubblesort helper


/ Published in: PHP
Save to your folder(s)

searching for a date sort function I found: http://slevy1.wordpress.com/2010/06/30/sorting-date-strings-in-php/ and built my codeigniter helper

call : $arrSortedDates = bubbleSort( $unsorted ); (previous->helper autoload)


Copy this code and paste it in your HTML
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. if ( ! function_exists('swapValues2')) {
  4. function swapValues2( $array, $dex, $dex2 ) {
  5. list($array[$dex],$array[$dex2]) = array($array[$dex2], $array[$dex]);
  6. return $array;
  7. }
  8. }
  9.  
  10. if ( ! function_exists('bubbleSort')) {
  11. function bubbleSort( $array)
  12. {
  13. for( $out=0, $size = count($array);$out < $size -1 ;$out++ )
  14. {
  15. for( $in = $out + 1;$in < $size;$in++)
  16. {
  17. if (strtotime($array[ $out ]) > strtotime($array[ $in ]))
  18. {
  19. $array = swapValues2($array, $out, $in);
  20. }
  21. }
  22. }
  23. return $array;
  24. }
  25. }
  26.  
  27.  
  28. /* End of file my_bubblesort_helper.php */
  29. /* Location: ./system/application/helpers/my_bubblesort_helper.php */

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.