Best way to sort arrays

This is a discussion on Best way to sort arrays within the PHP Language forums, part of the PHP Programming Forums category; Hi, On my page I oftend find myself presenting my data like this - all entries 1 - first (last modified) 2 - ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-31-2007
floortje
 
Posts: n/a
Default Best way to sort arrays

Hi,

On my page I oftend find myself presenting my data like this

- all entries
1 - first (last modified)
2 - second
3 - last

- last entries
3 - last

- last modified
1 - first (last modified)

I have found a couple of ways to solve this

The first is 3 different queries
$allentries=$db::select('SELECT * FROM entries')
$lastentries=$db::select('SELECT * FROM entries ORDER BY id DESC LIMIT 5')
$lastmodefiedentries=$db::select('SELECT * FROM entries ORDER BY
modefied DESC limit 5')

$allentries=$db::select('SELECT * FROM entries')
foreach($allentries as $val)
{
//
$lastentries[$val['id']]=$val;
$lastmodefiedentries[$val['modefied']]
}
// sort by id
krsort($lastentries)

// sort by modefied
krsort($lastmodefiedentries)

// etc

But I cant help wondering there should be a more efficient way to solve this.

In the first solution I use 2 extra queries and in the last soution I
use more system resources and it isn't really reusable.

Anyone know a better way ?

Floortje

Reply With Quote
  #2 (permalink)  
Old 11-01-2007
douggunnoe@gmail.com
 
Posts: n/a
Default Re: Best way to sort arrays

On Oct 31, 2:38 pm, floortje <n...@none.none> wrote:
> Hi,
>
> On my page I oftend find myself presenting my data like this
>
> - all entries
> 1 - first (last modified)
> 2 - second
> 3 - last
>
> - last entries
> 3 - last
>
> - last modified
> 1 - first (last modified)
>
> I have found a couple of ways to solve this
>
> The first is 3 different queries
> $allentries=$db::select('SELECT * FROM entries')
> $lastentries=$db::select('SELECT * FROM entries ORDER BY id DESC LIMIT 5')
> $lastmodefiedentries=$db::select('SELECT * FROM entries ORDER BY
> modefied DESC limit 5')
>
> $allentries=$db::select('SELECT * FROM entries')
> foreach($allentries as $val)
> {
> //
> $lastentries[$val['id']]=$val;
> $lastmodefiedentries[$val['modefied']]}
>
> // sort by id
> krsort($lastentries)
>
> // sort by modefied
> krsort($lastmodefiedentries)
>
> // etc
>
> But I cant help wondering there should be a more efficient way to solve this.
>
> In the first solution I use 2 extra queries and in the last soution I
> use more system resources and it isn't really reusable.
>
> Anyone know a better way ?
>
> Floortje


Could you not cut out the first query? Just in your second query lose
the 'limit' clause and then when you print "all entries", use the
$lastentries result. If ordered backwards, start from the last key and
iterate backwards, I suppose.

And then when you print part 2, just take the first 5.

Other than that, I would go with the 3 queries. Is there a need for it
to be much faster?

Reply With Quote
  #3 (permalink)  
Old 11-01-2007
floortje
 
Posts: n/a
Default Re: Best way to sort arrays

>
> Could you not cut out the first query? Just in your second query lose
> the 'limit' clause and then when you print "all entries", use the
> $lastentries result. If ordered backwards, start from the last key and
> iterate backwards, I suppose.
>
> And then when you print part 2, just take the first 5.
>
> Other than that, I would go with the 3 queries. Is there a need for it
> to be much faster?


I agree .... there are ways to work around the problem.

I could do something like:

echo 'Last 5 entries';
for ($i=count($allentries),$i>(count($allentries)-5);$i--)
{
echo $allentries[--$i].'<br />';
}

But It still bothers me that I cant simply extract 5 rows that have
largest/smallest value in an array field from a multi dimentional
array. Is there really no better way to do this ?

Floortje

Reply With Quote
  #4 (permalink)  
Old 11-01-2007
Michael Fesser
 
Posts: n/a
Default Re: Best way to sort arrays

..oO(floortje)

>I agree .... there are ways to work around the problem.
>
>I could do something like:
>
>echo 'Last 5 entries';
>for ($i=count($allentries),$i>(count($allentries)-5);$i--)
>{
>echo $allentries[--$i].'<br />';
>}
>
>But It still bothers me that I cant simply extract 5 rows that have
>largest/smallest value in an array field from a multi dimentional
>array. Is there really no better way to do this ?


For extracting a part of an array there is array_slice(), which should
work for your latest entries. For getting the modified ones you could
sort the array with a user-defined comparison function and then use
array_slice() again. You could also try array_filter(). The callback
function just has to check if the entry was modified.

Micha
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:17 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0