Jump to content


Photo

sorting an array


  • Please log in to reply
7 replies to this topic

#1 Jim from Oakland

Jim from Oakland
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 30 June 2006 - 06:17 PM

Phreax:

I need to sort the array based on the second dimension key named LName

Array is configured like this:

$dataArray[1]['UserID'] = 'dr45';
$dataArray[1]['FName'] = 'Joe';
$dataArray[1]['LName'] = 'Smith';

$dataArray[2]['UserID'] = 'qz50';
$dataArray[2]['FName'] = 'Huan';
$dataArray[2]['LName'] = 'Lee';

Thanks so much!



#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 30 June 2006 - 06:25 PM

Use custom sort function with usort()
function sortLName ($a, $b) {
	return strcmp($a['LName'], $b['LName']);
}

$dataArray[1]['UserID'] = 'dr45';
$dataArray[1]['FName'] = 'Joe';
$dataArray[1]['LName'] = 'Smith';

$dataArray[2]['UserID'] = 'qz50';
$dataArray[2]['FName'] = 'Huan';
$dataArray[2]['LName'] = 'Lee';

usort ($dataArray, 'sortLName');

echo '<pre>', print_r($dataArray, true), '</pre>';

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 Jim from Oakland

Jim from Oakland
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 30 June 2006 - 06:59 PM

Barand

That is great!  Thanks very much.

If you are inclined to share a bit more of your knowledge...

Can you give just a bit of explanation about what your approach does?

Also I generalized it so it will work with any key.

function sort_by_key ($key, $a, $b)
{
    return strcmp($a[$key], $b[$key]);
}

$dataArray[1]['UserID'] = 'dr45';
$dataArray[1]['FName'] = 'Joe';
$dataArray[1]['LName'] = 'Smith';

$dataArray[2]['UserID'] = 'qz50';
$dataArray[2]['FName'] = 'Huan';
$dataArray[2]['LName'] = 'Lee';

$dataArray[3]['UserID'] = 'mm61';
$dataArray[3]['FName'] = 'Have';
$dataArray[3]['LName'] = 'Hope';

$key = 'LName';

usort ($dataArray, sort_by_key($key));

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 30 June 2006 - 07:15 PM

Arguments for usort() are the array and the *name* of your sort function.

It passes two array elements at a time to this function ($a, $b) and you function should return

-1 if a sorts above b
0 if they are considered same
+1 if a sorts below b

As this is what strcmp() returns, it's a convenient function when all you need is a simple string comparison.

If you want to vary the key

function mySort ($a, $b) {

        global gl_$key;

        return strcmp($a[$gl_key], $b[$gl_key]);
}

$gl_key = 'FName';
usort ($dataArray, 'mySort');

If you want a DESC sort, return -strcmp()
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 Jim from Oakland

Jim from Oakland
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 30 June 2006 - 08:22 PM

Barand
OK. In the spirit of being brief I left out the fact that the array is a class property and that the sorting logic is for a method inthe same class. So, how do I tell usort about the function if "mySort" is a method in the same class as the array?  And, presumably the global scoping will work "within" the class?

class arrayManager
{

//loaded by caller
var $dataArray;

// data_array_sort method
function data_array_sort()
{
  return usort($this->dataArray, 'mySort');
}


function mySort($a, $b)
{
  global $gSortKey;
  strcmp($a[$gSortKey], $b[$gSortKey]);
}

}//end class


#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 30 June 2006 - 09:21 PM

Try it this way

class arrayManager
{

	//loaded by caller
	var $dataArray;
	var $gSortKey;
	
	function arrayManager($data) 
	{
		$this->dataArray = $data;
	}

	// data_array_sort method
	function data_array_sort($key)
	{
	  $this->gSortKey = $key;		
	  usort($this->dataArray, "arrayManager::mySort");
	  return $this->dataArray;
	}

	static function mySort($a, $b)
	{
	   return strcmp($a[$this->gSortKey], $b[$this->gSortKey]);
	}
	


}//end class

$dataArray[1]['UserID'] = 'dr45';
$dataArray[1]['FName'] = 'Joe';
$dataArray[1]['LName'] = 'Smith';

$dataArray[2]['UserID'] = 'qz50';
$dataArray[2]['FName'] = 'Huan';
$dataArray[2]['LName'] = 'Lee';

$dataArray[3]['UserID'] = 'mm61';
$dataArray[3]['FName'] = 'Have';
$dataArray[3]['LName'] = 'Hope';

$obj = new arrayManager($dataArray);

$sortedData = $obj->data_array_sort('FName');

echo '<pre>', print_r($sortedData, true), '</pre>';
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 Jim from Oakland

Jim from Oakland
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 30 June 2006 - 11:14 PM

Thanks again

I appreciate your assistance, a lot. Unfortunately it is still not working; the array is reordered but not sorted.

I note that the static keyword fails on my system; I'm using 4.3.

I modified code just a bit to match my class's names.

<?
class TableData
{
var $dataTableArray;
var $sortKey;

function TableData($data)
{
    $this->dataTableArray = $data;
}

function data_array_sort($key)
{
  $this->sortKey = $key;
  usort($this->dataTableArray, "TableData::mySort");
  return $this->dataTableArray;
}

function mySort($a, $b)
{
  return strcmp($a[$this->sortKey], $b[$this->sortKey]);
}
}//end class

$dataArray[1]['UserID'] = 'dr45';
$dataArray[1]['FName'] = 'Joe';
$dataArray[1]['LName'] = 'Smith';

$dataArray[2]['UserID'] = 'qz50';
$dataArray[2]['FName'] = 'Huan';
$dataArray[2]['LName'] = 'Lee';

$dataArray[3]['UserID'] = 'aw13';
$dataArray[3]['FName'] = 'Zoey';
$dataArray[3]['LName'] = 'Zeus';

$dataArray[4]['UserID'] = 'mm61';
$dataArray[4]['FName'] = 'Have';
$dataArray[4]['LName'] = 'Hope';

$obj = new TableData($dataArray);

$sortedData = $obj->data_array_sort('LName');

echo '<pre>', print_r($sortedData, true), '</pre>';
?>

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 01 July 2006 - 12:04 AM

The only way I can get it to work with v4 is to specify the key as a literal in the sort function

class TableData
{
   var $dataTableArray;
   var $sortKey;
   
   function TableData($data)
   {
       $this->dataTableArray = $data;
   }

   function data_array_sort($key)
   {
     $this->sortKey = $key;
     usort($this->dataTableArray, array('TableData','mySort'));
     return $this->dataTableArray;
   }

   function mySort($a, $b)
   {   	   
      return strcmp($a['LName'], $b['LName']);
   }
}//end class

$dataArray[1]['UserID'] = 'dr45';
$dataArray[1]['FName'] = 'Joe';
$dataArray[1]['LName'] = 'Smith';

$dataArray[2]['UserID'] = 'qz50';
$dataArray[2]['FName'] = 'Huan';
$dataArray[2]['LName'] = 'Lee';

$dataArray[3]['UserID'] = 'aw13';
$dataArray[3]['FName'] = 'Zoey';
$dataArray[3]['LName'] = 'Zeus';

$dataArray[4]['UserID'] = 'mm61';
$dataArray[4]['FName'] = 'Have';
$dataArray[4]['LName'] = 'Hope';

$obj = new TableData($dataArray);

$sortedData = $obj->data_array_sort('LName');

echo '<pre>', print_r($sortedData, true), '</pre>';

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users