Jump to content

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!

Link to comment
https://forums.phpfreaks.com/topic/13324-sorting-an-array/
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]
Link to comment
https://forums.phpfreaks.com/topic/13324-sorting-an-array/#findComment-51369
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));
Link to comment
https://forums.phpfreaks.com/topic/13324-sorting-an-array/#findComment-51385
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()
Link to comment
https://forums.phpfreaks.com/topic/13324-sorting-an-array/#findComment-51395
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
Link to comment
https://forums.phpfreaks.com/topic/13324-sorting-an-array/#findComment-51418
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]
Link to comment
https://forums.phpfreaks.com/topic/13324-sorting-an-array/#findComment-51443
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>';
?>
Link to comment
https://forums.phpfreaks.com/topic/13324-sorting-an-array/#findComment-51463
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]
Link to comment
https://forums.phpfreaks.com/topic/13324-sorting-an-array/#findComment-51471
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.