Jump to content

help converting an array sort function into a class


emehrkay

Recommended Posts

I found this very simple multi dimensional array sorting function that I figured would work better as a class (no global vars, extendable, etc).

 

<?php
// demo array to sort 
$latest_array = array( 
  array('news','1234567890','sdf','asdpojq'), 
  array('news','1234567892','uruy','xbuqiwpo'), 
  array('comment','1234567893','fghj','mjktyu'), 
  array('article','1234567891','cvmo','pjklgg'), 
  array('news','1234567894','qwenb','asbhtyhj'), 
); 

$sort_field = 0; // enter the number of field to sort 

// compare function 
function cmpi($a, $b) 
{ 
     global $sort_field; 
     return strcmp($a[$sort_field], $b[$sort_field]); 
} 

// do the array sorting 
usort($latest_array, 'cmpi'); 

// demo output 
echo '<pre>'; 
print_r($latest_array); 
echo '</pre>'; 
?>

 

I figured that this owuld be a straight forward conversion. Make the global a class property defined in the constructor, and the function call, a call to a method.

 

Here is what I came up with

 

<?php

/**
* 
*/
class Utilities_Array_Sort{
    private $_array;
    private $_sort_key;
    
    /**
     * Class Constructor
     *
     * @access public
     * @return null
     */     
    public function __construct($array, $sort_key){
        $this->_array       = $array;
        $this->_sort_key    = $sort_key;
    }
    
    /**
     * sorts the array 
     *
     * @access public
     * @return RETURN TYPE
     */
    public function sort($direction = 'ascending'){
        switch($direction){
            case 'asc':
            case 'ascending':
                usort($this->_array, '$this->ascending');
                break;
                
            case 'desc':
            case 'descending':
                
                break;
        }
        
        return $this->_array;
    }
    
    /**
     * sorts the array in ascending order
     *
     * @access private
     * @return int
     */
    private function ascending($a, $b){
        return strcmp($a[$this->_sort_key], $b[$this->_sort_key]); 
    }
}

?>

 

When I go to use it, I get an error telling me that I am using an invalid comparison function

 

Invalid comparison function

 

Here is my usage

 

<?php
// demo array to sort 
$latest_array = array( 
  array('news','1234567890','sdf','asdpojq'), 
  array('news','1234567892','uruy','xbuqiwpo'), 
  array('comment','1234567893','fghj','mjktyu'), 
  array('article','1234567891','cvmo','pjklgg'), 
  array('news','1234567894','qwenb','asbhtyhj'), 
);


$sort = new Utilities_Array_Sort($latest_array, 0);
$x = $sort->sort();

// demo output 
echo '<pre>'; 
print_r($x); 
echo '</pre>';
?>

 

What am I missing?

 

Thanks

Geeze, the answer was right on the usort page on php.net.

 

I need to pass an array with the class and method as the vars instead of just the method name

 

usort($this->_array, array('Utilities_Array_Sort', 'ascending'));

 

Thanks anyway

If anyone is interested, here is the class that I came up with.

 

<?php
<?php

/**
* 
*/
class Utilities_Array_Sort{
    /**
     * @param _array array array of data to be sorted
     * @access private
     */
    private $_array;
    
    /**
     * @param _sort_key String the key to sort the array by
     * @access private
     */
    private $_sort_key;
    
    /**
     * Class Constructor
     *
     * @access public
     * @return null
     */     
    public function __construct(array $array = array(), $sort_key = null){
        $this->_array       = $array;
        $this->_sort_key    = $sort_key;
    }
    
    /**
     * sorts the array 
     *
     * @param $direction String Direction that you would like to sort the array by
     * @access public
     * @return RETURN TYPE
     */
    public function sort($direction = 'ascending'){
        switch($direction){
            case 'asc':
            case 'ascending':
                usort($this->_array, array('Utilities_Array_Sort', 'ascending'));
                break;
                
            case 'desc':
            case 'descending':
                usort($this->_array, array('Utilities_Array_Sort', 'descending'));
                break;
        }
        
        return $this->_array;
    }
    
    /**
     * sets a new array
     *
     * @access public
     * @return instance of Utilities_Array_Sort class
     */
    public function setArray(array $array){
        $this->_array = $array;
        
        return $this;
    }
    
    /**
     * sets a new sort by key
     *
     * @access public
     * @return instance of Utilities_Array_Sort class
     */
    public function setSortKey($sort_key = false){
        if($sort_key) $this->_sort_key = $sort_key;
        
        return $this;
    }
    
    /**
     * sorts the array in ascending order
     *
     * @access private
     * @return int
     */
    private function ascending($a, $b){
        return strcmp($a[$this->_sort_key], $b[$this->_sort_key]); 
    }
    
    /**
     * sorts the array in descending order
     *
     * @access private
     * @return int
     */
    private function descending($a, $b){
        return strcmp($a[$this->_sort_key], $b[$this->_sort_key]) * -1; 
    }
}
?>

?>

 

and usage

 

<?php
// demo array to sort 
$latest_array = array( 
  array('news','1234567890','sdf','a'), 
  array('news','1234567892','uruy','b'), 
  array('comment','1234567893','fghj','c'), 
  array('article','1234567891','cvmo','d'), 
  array('news','1234567894','qwenb','e'), 
);

$arr2 = array(
    array('a' => 'EH', 'b' => 'BEE'),
    array('a' => 'first', 'b' => 'second'),
    array('a' => 'one', 'b' => 'two'),
    array('a' => 1, 'b' => 2)
);

$sort = new Utilities_Array_Sort($latest_array, 3);
$x = $sort->sort('desc');

echo '<pre>'; 
print_r($x); 
echo '</pre>';

/**
* overwrite the array and key and sort descending
*/
$y = $sort->setArray($arr2)->setSortKey('a')->sort('desc');

echo '<pre>'; 
print_r($y); 
echo '</pre>';

/**
* use the same array and key but sort it ascending
*/
$y = $sort->sort();

echo '<pre>'; 
print_r($y); 
echo '</pre>';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.