Sorting multiple XML data by date

Sorting multiple XML data by date

am 15.08.2007 06:08:26 von junkmate

I am making an RSS parser that takes multiple XML inputs in to an
array and then sorts them by their date value... and it 'almost'
works... I always lose the top value of the second rss feed to be
parsed... its pretty annoying as this is often the most recently
updated RSS item that is missing from my multi-feed...

Help me please:


Currently, I am doing this:

usort($xmlreader_parsed_data, "rss_cmp");

which calls this function:

function rss_cmp($a, $b)
{
$a = strtotime($a->date);
$b = strtotime($b->date);

if ($a == $b) {
return 0;
}
return ($a > $b) ? -1 : 1;
}

Re: Sorting multiple XML data by date

am 15.08.2007 06:12:16 von junkmate

UPDATE: I just added the following to the rss_cmp function, and it
seems to have worked... is it ok? Or will I regret this somewhere?

if (!$a) { return -1; }
if (!$b) { return 1; }

Re: Sorting multiple XML data by date

am 17.08.2007 03:10:59 von petersprc

Seems to be a typo, comparison should be $a < $b.

I would make sure error_reporting(E_ALL) is on to see if that gives
any hints.

On Aug 15, 12:08 am, junkmate wrote:
> I am making an RSS parser that takes multiple XML inputs in to an
> array and then sorts them by their date value... and it 'almost'
> works... I always lose the top value of the second rss feed to be
> parsed... its pretty annoying as this is often the most recently
> updated RSS item that is missing from my multi-feed...
>
> Help me please:
>
> Currently, I am doing this:
>
> usort($xmlreader_parsed_data, "rss_cmp");
>
> which calls this function:
>
> function rss_cmp($a, $b)
> {
> $a = strtotime($a->date);
> $b = strtotime($b->date);
>
> if ($a == $b) {
> return 0;
> }
> return ($a > $b) ? -1 : 1;
> }

Re: Sorting multiple XML data by date

am 17.08.2007 04:22:48 von junkmate

putting the coparison that way gives me results the wrong way around.
I want the latest date at the top (ie. the smallest) and to do that I
have to use > - shouldnt cause a problem though right?




On Aug 17, 2:10 am, petersprc wrote:
> Seems to be a typo, comparison should be $a < $b.
>
> I would make sure error_reporting(E_ALL) is on to see if that gives
> any hints.
>
> On Aug 15, 12:08 am, junkmate wrote:
>
> > I am making an RSS parser that takes multiple XML inputs in to an
> > array and then sorts them by their date value... and it 'almost'
> > works... I always lose the top value of the second rss feed to be
> > parsed... its pretty annoying as this is often the most recently
> > updated RSS item that is missing from my multi-feed...
>
> > Help me please:
>
> > Currently, I am doing this:
>
> > usort($xmlreader_parsed_data, "rss_cmp");
>
> > which calls this function:
>
> > function rss_cmp($a, $b)
> > {
> > $a = strtotime($a->date);
> > $b = strtotime($b->date);
>
> > if ($a == $b) {
> > return 0;
> > }
> > return ($a > $b) ? -1 : 1;
> > }

Re: Sorting multiple XML data by date

am 17.08.2007 05:49:10 von petersprc

Yes you're definitely right. I thought that might the cause of the
unexpected results. Not sure why data would go missing. I would see if
any warnings are triggered or I guess strtotime could also fail.

On Aug 16, 10:22 pm, junkmate wrote:
> putting the coparison that way gives me results the wrong way around.
> I want the latest date at the top (ie. the smallest) and to do that I
> have to use > - shouldnt cause a problem though right?
>
> On Aug 17, 2:10 am, petersprc wrote:
>
> > Seems to be a typo, comparison should be $a < $b.
>
> > I would make sure error_reporting(E_ALL) is on to see if that gives
> > any hints.
>
> > On Aug 15, 12:08 am, junkmate wrote:
>
> > > I am making an RSS parser that takes multiple XML inputs in to an
> > > array and then sorts them by their date value... and it 'almost'
> > > works... I always lose the top value of the second rss feed to be
> > > parsed... its pretty annoying as this is often the most recently
> > > updated RSS item that is missing from my multi-feed...
>
> > > Help me please:
>
> > > Currently, I am doing this:
>
> > > usort($xmlreader_parsed_data, "rss_cmp");
>
> > > which calls this function:
>
> > > function rss_cmp($a, $b)
> > > {
> > > $a = strtotime($a->date);
> > > $b = strtotime($b->date);
>
> > > if ($a == $b) {
> > > return 0;
> > > }
> > > return ($a > $b) ? -1 : 1;
> > > }