Jump to content

sort=-id not working


leoric
 Share

Recommended Posts

                <?php              
                    $matches=$pages->find("parent_id=$page->id, limit=5,sort=-id");
                    $pagination = $matches->renderPager();
                    echo $pagination;
                       echo '<ul class=rows>';
                    foreach($matches as $item)
                    {
                           echo '<li><a href="'.$item->url.'">'.$item->title.'</a></li>';                    
                        }
                    echo '</ul>';
                    echo $pagination;                         
                 ?>   

hi , this is my code , the problem is  "sort=-id" isnot working 

this is some articles in many pages.

when i use "$matches=$pages->find("parent_id=$page->id, limit=5)->sort("-id");" 

then i found the articles order by -id in current  page,but i want to  them work in all of the pages.

and if i use "sort=-title/title" replace "sort=id/-id" ,its working perfect.

whats wrong with this? thank you !

Link to comment
Share on other sites

I'm also having problems following your question; the part about finding articles "in current page" vs. "in all of the pages" confuses me, as you've specified in your selector that you only want articles with current page as their parent. Obviously you'll only find articles below current page this way (and the syntax Adrian suggested is more straightforward way to achieve the same.)

Anyway, that aside, I can't seem to reproduce your issue either. Sorting by id/-id provides results in opposite order, which kind of signals that those work as intended.

What kind of results are you getting, ie. how did you realize that sort=id/-id doesn't work?

Link to comment
Share on other sites

Maybe I am not following what you need exactly, but I think this might work for you:

$matches = $page->children("limit=5,sort=-id");

thank you for you reply,adrian

in the fact,i try do this earlier,but the problem is still  :(

Link to comment
Share on other sites

@adrian @teppo

i am sorry for my expression is not clear.  and i want to do like this

article id:  1 2 3 4  5  6  7 8 9 10  11 12 ...

page 1

12 11 10 9

page 2

8 7 6 5 

page 3

4 3 2 1

...

but when i use this code

"$matches=$pages->find("parent_id=$page->id, limit=5,sort=-id");" or "$matches = $page->children("limit=5,sort=-id");"

its not working..

if i use "$matches=$pages->find("parent_id=$page->id, limit=5")->sort("-id")";

the result will be this:

article id:  1 2 3 4  5  6  7 8 9 10  11 12 ...

page 1

4 3 2 1

page 2

8 7 6 5

page 3

12 11 10 9

...

Link to comment
Share on other sites

Thanks for the details explanation - the behavior you are seeing when using ->sort("-id") is expected and working as intended. I don't really understand why the other approach is not working as it should.

I am not sure how you are comparing the output titles to the IDs, so maybe just to be sure can you try: 

echo '<li><a href="'.$item->url.'">'.$item->title.' ('.$item->id.')</a></li>'; 

Just in case the your expected order is not actually based on the ID, but rather on your manually sorted order. Which is also why I suggested the -sort option. Sorry if you do have this right - sometimes it can be easy to overlook the simplest things. Maybe also try the selector in the selector test module which will output all the information about each page, including the ID.

Link to comment
Share on other sites

Thanks for the details explanation - the behavior you are seeing when using ->sort("-id") is expected and working as intended. I don't really understand why the other approach is not working as it should.

I am not sure how you are comparing the output titles to the IDs, so maybe just to be sure can you try: 

echo '<li><a href="'.$item->url.'">'.$item->title.' ('.$item->id.')</a></li>'; 

Just in case the your expected order is not actually based on the ID, but rather on your manually sorted order. Which is also why I suggested the -sort option. Sorry if you do have this right - sometimes it can be easy to overlook the simplest things. Maybe also try the selector in the selector test module which will output all the information about each page, including the ID.

i am back...

omg,its working

i dont changed something,just do as you say : add "$item->id" then i found its work correctly 

its a big joke,i think if the cache caused?  my work space in localhost (appserv + win7 x64)

@adrian thank you very much!

  • Like 1
Link to comment
Share on other sites

I'm not sure what the value of sorting by "id" is, but if it's for chronological reasons, consider using "created" instead as your sort field. That's going to be more reliable to achieve the same purpose.  

  • Like 2
Link to comment
Share on other sites

  • 6 months later...

I'm not sure what the value of sorting by "id" is, but if it's for chronological reasons, consider using "created" instead as your sort field. That's going to be more reliable to achieve the same purpose.  

Reviving this old topic....

For pages created in bulk using the API, many will have the same timestamp (e.g. batch create 1000 pages using the API); the only unique thing for such pages will be the ID. If I limit my selector to one template only, the ID then becomes a more reliable get selector to use with < or >. The assumption here is that since IDs are incremented, a greater ID denotes a later created time. In bulk page creation then, the timestamp is less reliable and will return the same result every time which is of no good use when creating next/prev links :-)...maybe I'm missing something?  :)

  • Like 3
Link to comment
Share on other sites

I tend to sort by created then by id in this case.

Or if you're using the API to import something then overwrite the created time (can't remember if you can do that but certainly you can use another field).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...