Jump to content

Archived

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

Jim from Oakland

sorting an array

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Use custom sort function with usort()
[code]
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>';[/code]

Share this post


Link to post
Share on other sites
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));

Share this post


Link to post
Share on other sites
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()

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Try it this way

[code=php:0]
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>';
?>[/code]

Share this post


Link to post
Share on other sites
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>';
?>

Share this post


Link to post
Share on other sites
The only way I can get it to work with v4 is to specify the key as a literal in the sort function

[code=php:0]
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>';[/code]

Share this post


Link to post
Share on other sites

×

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.