PDA

View Full Version : Array sorting issue


abduraooft
03-01-2008, 02:34 PM
Hi all,
I've a 2D array,
$Items=array(
array('date'=>'2008-02-16 06:54:03','name'=>'item1'),
array('date'=>'2007-12-20 14:47:36','name'=>'item2'),
array('date'=>'2007-12-01 13:23:46','name'=>'item3'),
array('date'=>'2007-12-01 05:26:38','name'=>'item4'),
array('date'=>'2007-11-28 10:51:53','name'=>'item5'),

array('date'=>'2008-02-01 09:20:45','name'=>'item6'),
array('date'=>'2008-01-11 09:23:29','name'=>'item2'),
array('date'=>'2007-12-03 09:11:14','name'=>'item4'),
array('date'=>'2007-12-03 03:32:51','name'=>'item3'),
array('date'=>'2007-11-30 07:00:40','name'=>'item5')
);
How can I sort this in the order of date ASC. (It's actually the combined result of 2 queries, so unable to populate using mysql)

How can I eliminate the repeating items ? I just want only 5 latest among them. Should I use another array(to store selected) and loop and in_array() function for that?

regards,

CFMaBiSmAd
03-01-2008, 02:54 PM
If you show the queries, it is likely that a UNION, then using normal sql statements, you can achieve what you want directly in a single query.

abduraooft
03-19-2008, 04:15 PM
First of all sorry for the late reply, I've been thinking about redesigning the DB and php logic, but I'm afraid, this couldn't be completed in near future :-( . So let me try to fix with the current setup.

I've a table smsCREATE TABLE `sms` (
`mid` int(11) NOT NULL auto_increment,
`sub` varchar(70) ,
`date` datetime ,
`msg` text,
`groupid` int(11) ,
`parent` int(11) ,
`categoryid` int(11) ,
PRIMARY KEY (`mid`),
KEY `parent` (`parent`),
FULLTEXT KEY `sub` (`sub`),
FULLTEXT KEY `msg` (`msg`)
)
(omitted some irrelevant fields), where
groupid specifies the group/subgroup of a row, will be a primary key of another table group
categoryid
equal to 1 => Original Post

greater than 1 => Replies

parent will be mid of an OP, in the case of a Reply

Now I want to find out the current active threads in the descending order of their last activity (if no reply, then consider the date of OP, else date of last reply).

I know this can be effectively tracked by an extra table by storing the mid of each OP along with a date, but :-(

I just want only 5 active one, so I thought about first getting 5 OPs in the order of posted date, and then select a set of 5 distinct OPs based on the dates of replies, and merge, then sort and filter.... my god!
Any simple and effective way? Any help would be appreciated.

daemonkin
03-19-2008, 05:27 PM
Hi,

I found and have adapted the following to use in a sports standings table:


function arfsort($a, $fl)
{
$GLOBALS['__ARFSORT_LIST__'] = $fl;
usort($a, 'arfsort_func');
return $a;
}
function arfsort_func($a, $b)
{
foreach($GLOBALS['__ARFSORT_LIST__'] as $f) {
switch ($f[1]) { // switch on ascending or descending value
case "d":
if (is_numeric($b[$f[0]]) && is_numeric($a[$f[0]])) {
if ($b[$f[0]] - $a[$f[0]] < 0) {
return -1;
} else if ($b[$f[0]] - $a[$f[0]] > 0) {
return 1;
}
} else {
$strc = strcmp(strtolower($b[$f[0]]), strtolower($a[$f[0]]));
if ($strc != 0) {
return $strc;
}
}
break;
default:
if (is_numeric($a[$f[0]]) && is_numeric($b[$f[0]])) {
if ($a[$f[0]] - $b[$f[0]] < 0) {
return -1;
} else if ($a[$f[0]] - $b[$f[0]] > 0) {
return 1;
}
} else {
$strc = strcmp(strtolower($a[$f[0]]), strtolower($b[$f[0]]));
if ($strc != 0) {
return $strc;
}
}
break;
}
}
return 0;
}


I then run:


$order_arr =
array( // d means decending - swap for 'a' to see effect
array('points','d'),
array('goal_difference','d'),
array('for','d'),
array('against','d'),
array('teamName','a')
);
$sorted = arfsort($aTeams, $order_arr);
$aTeams = $sorted;


If you change the order array elements for those in your array and use asc or desc you can then obtain your desired results.

Hope this helps or makes sense.

D.

Inigoesdr
03-19-2008, 09:50 PM
Moved at OP's request.

StupidRalph
03-21-2008, 07:38 AM
Hi all,
I've a 2D array,
$Items=array(
array('date'=>'2008-02-16 06:54:03','name'=>'item1'),
array('date'=>'2007-12-20 14:47:36','name'=>'item2'),
array('date'=>'2007-12-01 13:23:46','name'=>'item3'),
array('date'=>'2007-12-01 05:26:38','name'=>'item4'),
array('date'=>'2007-11-28 10:51:53','name'=>'item5'),

array('date'=>'2008-02-01 09:20:45','name'=>'item6'),
array('date'=>'2008-01-11 09:23:29','name'=>'item2'),
array('date'=>'2007-12-03 09:11:14','name'=>'item4'),
array('date'=>'2007-12-03 03:32:51','name'=>'item3'),
array('date'=>'2007-11-30 07:00:40','name'=>'item5')
);
How can I sort this in the order of date ASC. (It's actually the combined result of 2 queries, so unable to populate using mysql)

How can I eliminate the repeating items ? I just want only 5 latest among them. Should I use another array(to store selected) and loop and in_array() function for that?

regards,

Couldn't you just UNION the two queries and have that take care of combining the two queries and eliminating the repeating items?

abduraooft
06-20-2008, 01:05 PM
Couldn't you just UNION the two queries and have that take care of combining the two queries and eliminating the repeating items? yes, UNION was the thing required for me! Thanks a lot.