Jump to content

usort not working properly


millercj

Recommended Posts

I made a custom sort to reorder a multidimensional array of data pulled from a database. It seems to be sorting letters fine but doesn't work with numbers...which is what i need. If you can offer any suggestions they'd be appreciated.

 

	$sort_field = 5;
	function cmpi($a,$b) 
	{ 
		 global $sort_field; 
		 return strcmp($a[$sort_field], $b[$sort_field]); 
	} 
	usort($cartList, 'cmpi'); 

Link to comment
Share on other sites

Sure,I'll make a chart like thing

 

 

Sort Order        $cartList

      1                    36

      3                    40

      4                    43

      5                    46

      6                    50

Link to comment
Share on other sites

There are built in sorting functions for array in PHP.

 

If multidimensional array: array_multisort()

 

There are a lot of functions to check at:

 

sort(), arsort(), asort(), ksort(), krsort(), natsort(), natcasesort(), rsort(), usort(), array_multisort(), and uksort().

 

Does any of these work with what you need?

 

Link to comment
Share on other sites

oh my bad...

 

tr this instead, we will be dissecting it though:

 

<?php
// given they have something like this
$data = array();

$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7);

// Obtain a list of columns
foreach ($data as $key => $row) {
    $volume[$key]  = $row['volume'];
    $edition[$key] = $row['edition'];
}

// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
?> 

 

can check this out? They should have common keys here... you can have an idea from here.

Link to comment
Share on other sites

this is what I did...no errors but still not sorting it right though

 

foreach ($cartList as $key => $row) {
    $name[$key]  = $row[0];
    $price[$key] = $row[1];
    $qty[$key]  = $row[2];
    $sizedesc[$key] = $row[3];
    $image[$key]  = $row[4];
    $date[$key] = $row[5];
}

array_multisort($date, SORT_DESC, $cartList);

Link to comment
Share on other sites

this one really works fine:

 

<?php
// given they have something like this
$data = array();

$data[] = array(0 => 67, 1 => 2);
$data[] = array(0 => 86, 1 => ;
$data[] = array(0 => 85, 1 => 6);
$data[] = array(0 => 98, 1 => 2);
$data[] = array(0 => 86, 1 => 6);
$data[] = array(0 => 67, 1 => 7);

// Obtain a list of columns
foreach ($data as $key => $row) {
    $volume[$key]  = $row[0];
    $edition[$key] = $row[1];
}

// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($volume, SORT_DESC, SORT_REGULAR, $edition, SORT_ASC, $data);

print_r($data);
?>

 

may i know the structure of your array?

Link to comment
Share on other sites

<?PHP
$cartList[$i] [0] = $nextresult['name'];
$cartList[$i] [1] = $nextresult['price'];
$cartList[$i] [2] = $nextresult['QTY'];
$cartList[$i] [3] = $nextresult['sizeDesc'];
$cartList[$i] [4] = $nextresult['image'];
$cartList[$i] [5] = $nextresult['date'];
?>

Link to comment
Share on other sites

so I presume you can d o something like this:

 

<?php
// all other code... given $resource is the result of mysql_query()
$cartList = array();

while($nextresult = mysql_fetch_assoc($resource)) {
    $car = array();

    $car[0] = $nextresult['name'];
    $car[1] = $nextresult['price'];
    $car[2] = $nextresult['QTY'];
    $car[3] = $nextresult['sizeDesc'];
    $car[4] = $nextresult['image'];
    $car[5] = $nextresult['date'];

    $cartList[] = $car;
}

foreach($cartList as $key => $row) {
    $name[$key]  = $row[0];
    $price[$key] = $row[1];
    $qty[$key]  = $row[2];
    $sizedesc[$key] = $row[3];
    $image[$key]  = $row[4];
    $date[$key] = $row[5];
}

array_multisort($date, SORT_DESC, SORT_REGULAR, $cartList);?>

 

one question though, you are sorting it by date right? I don't think it is really necessary to sort it again.

 

And I think the problem you had is because you are sorting it with date... which I guess you need to make a custom one for that... except you add another field of date which is in unix timestamp and sort it with that as numeric. Apart from that, I still suggest not to sort it in PHP since you can sort it in SQL with date and knowing that changing the title doesn't really matter much with that, since you said you want to sort it with the sixth column.

Link to comment
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.