Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+14 votes
2k views
in Plugins by
edited by

I have developed one plugin for sorting answers in a qa question page . Which enables user to sort the answers by oldest , newest and highest voted filters . 

I have done this without any core hacks , without any ajax calls . Just it is a dom manipulation . I will release the plugin as soon as I finish . 

Features - Very simple , fast and powerfull 

Download link - http://q2astore.com/product/answer-sort-plugin/

Limitation - this sorts only the answers on the current page . This is a simple plugin does not sorts the  paginated answers . 

 

Cheers .. 

Q2A version: 1.6.3
by
Nice try. Q2A core extracts all answers from database. However, because Q2A core sort and cut off from answer array in view layer, it may be difficult to realize without core hack.
by
Thanks sama .
yah for that , I will grab answers in layer and dump it somewhere in the body as a hidden element . That will be sorted and reordered as per requirement in the main area . I agree I need to make another query to db for all the answers again , may be cost a db query , but after that I can access them as many times I want with out any more db calls , less costiler than anoter page load or a ajax call  .
Please let me know if you have any better idea for the implementation .
by
edited by
If it is me ... (Page load is necessary)

1) Make new plugin.
2) Copy from qa-page-question.php to qa-page-question-user-sort.php
3) Override qa_get_request_content() of qa-page.php
  Example:
    ...
    $enable_user_sort = qa_opt('xxxx');
    if($enable_user_sort)
      $qa_content=require QA_INCLUDE_DIR.'qa-page-question-user-sort.php';
    else
      $qa_content=require QA_INCLUDE_DIR.'qa-page-question.php';
4) Read $sortkey from cookie in qa-page-question-user-sort.php
5) Change qa_sort_by($answers, $sortkey); in qa-page-question-user-sort.php
by
Yes sama , you are correct . Page load is necessary to sort all the answers . Otherwise with only DOM manipulations the plugin becomes unstable . So as of now I will keep the plugin very simple that sorts the answers of the current page only . Thanks .

3 Answers

+6 votes
by
Yeah, I also think this should be present in the core. And actually it is part of the v1.7 backlog, which means (unless there is an unexpected scope change) this feature should be added to the v1.7 release. Anyway, I want to share a few thoughts on this.

The issue about writing this kind of plugin without making core changes and no AJAX calls is that you will only be able to sort questions on a given page. In other words, suppose you have 50 answers for a question, you display 10 answers per page, you are in the second page and the core is configured to display answers by date. If I configure the plugin to sort by highest votes then I will still get exactly all the same answers (11-20 in the date sort) but only those 11-20 sorted by vote.

Or maybe a simpler example, if the core is configured to sort by date and the most voted answer of a question is not in the first page, then I wont' see it if the plugin is set to sort by highest votes.

So this behaviour is a bit unexpected. However, this plugin would still be 100% reliable if you apply a simple rule: you only display the plugin buttons if and only if all the answers for a question fit in the first page. Of course, the downside of applying this rule is that users will sometimes see the buttons and sometimes not, depending on the amount of answer pages.

I just thought sharing this might help on the development :)
by
Hi Gabriel , Thanks for sharing your thoughts with us . I already kept this thing in my mind . And I still think it is possible to implement these features without any ajax calls or without any extra page loades . Lets see . I will update you soon once I finish . :) . Thanks you again
by
You were correct . With dom manipulation I am able to make the plugin working will sorting all questions . But the ajax operations becomes unstable , and invites more bugs . So I am removing the feature of sorting answers from multiple pages . It will only sort answers of the current page .
Thanks
by
Yeah, that would be the most reliable solution, IMO. Furthermore I guess most questions will have all of their answers fit in one page which would be barely noticeable the fact that it is only sorting on those cases.
by
IMO the idea of sorting the answers comes handy only if too many answers were given which hides some from first page and pushes them in further pages.
by
yeah , that is correct . Most of the questions has answers on a single page . This plugin works pretty fast for that without spending any extra resources . If i will get from other pages , it would need some ajax calls or a new page load . I will think about a better solution in the next version . Thanks for the suggestion .
+2 votes
by
by
this is awesome thanks very much!!!
by
you are welcome . Thanks for the review . :)
by
edited by
Nice plugin. But, I have one question. When answer count exceeds "'page_size_q_as'" option, how does this plugin behave?

[Edit] Sorry, I overlooked your upper comment.
---
Page load is necessary to sort all the answers . Otherwise with only DOM manipulations the plugin becomes unstable . So as of now I will keep the plugin very simple that sorts the answers of the current page only.
---
I recommend that you write it in Github as restrictions.
by
Yah , I have updated . Thanks ..
0 votes
by
not working with 1.7.4...:( any help, please? :)
...