PHP Classes
elePHPant
Icontem

File: example.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Gerd Christian Kunze  >  AIO:Smoothsort  >  example.php  >  Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example
Class: AIO:Smoothsort
Sort values using the SmoothSort algorithm
Author: By
Last change:
Date: 4 years ago
Size: 1,736 bytes
 

Contents

Class file image Download
<?php
// SmoothSort Algorithm
require_once('module.smoothsort.php');

// Some simple example :o)
$array_data = array( 3, 2, 4, 1 );
cls__module_smoothsort::smoothsort_run( $array_data );
var_dump( $array_data );

/*
Result:
array(4) {
  [0]=>int(1)
  [1]=>int(2)
  [2]=>int(3)
  [3]=>int(4)
}
*/

// Now where the magic evolves
class foo {
    private
$class_field = null;
    function
__construct( $class_field ) {
       
$this->class_field( $class_field );
    }
    function
class_field( $class_field = null ) {
        if(
$class_field !== null ) $this->class_field = $class_field;
        return
$this->class_field;
    }
}

function
sort_foo_ascending_on_class_field( $foo_a, $foo_b ) {
   
// Swap foo a&b in array ?
   
if( $foo_a->class_field() < $foo_b->class_field() ) return true;
   
// or don't swap
   
return false;
}

$array_data = array(
    new
foo( 1 ),
    new
foo( 4 ),
    new
foo( 6 ),
    new
foo( 2 ),
    new
foo( 3 ),
    new
foo( 5 )
);

cls__module_smoothsort::smoothsort_run( $array_data, 'sort_foo_ascending_on_class_field' );
var_dump( $array_data );

/*
Result:
array(6) {
  [0]=>object(foo)#1 (1) {
    ["class_field":"foo":private]=>
    int(1)
  }
  [1]=>object(foo)#4 (1) {
    ["class_field":"foo":private]=>
    int(2)
  }
  [2]=>object(foo)#5 (1) {
    ["class_field":"foo":private]=>
    int(3)
  }
  [3]=>object(foo)#2 (1) {
    ["class_field":"foo":private]=>
    int(4)
  }
  [4]=>object(foo)#6 (1) {
    ["class_field":"foo":private]=>
    int(5)
  }
  [5]=>object(foo)#3 (1) {
    ["class_field":"foo":private]=>
    int(6)
  }
}
*/

// Ok, ok there is something called usort() doing this trick...
// So this was just "for fun", doing SmoothSort Algorithm in PHP ;o)
?>