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

In order to provide a better user experience in my forum, I would like to =>

Sort Questions in TAG LISTS By Most Votes? (and/or most answers)

So the user will have when clicking tag "ABC" =>

question1 : 23votes - 15 answers
question2 : 18votes - 11 answers
question3 : 13votes - 8 answers
question4 : 11votes - 7 answers

(Instead of)  =>
question1 : 0votes - 0 answers
question2 : 1votes - 0 answers
question3 : 0votes - 1 answers
question4 : 11votes - 7 answers

by
+++

In my opinion better questions sorting in lists are one of the most waited features.

http://www.question2answer.org/qa/31514/what-do-you-want-in-question2answer-1-7?show=31526#a31526

A lot of requests here in q2a forum about similar feature !
by
+++ agreed we need this to have better results!
by
Nobody to help??
by
The sorting must be done by one criteria. You example is tricky because the most voted ones happen to match the most answered ones. You could have these data:
question1 : 23votes - 7 answers
question2 : 18votes - 8 answers
question3 : 13votes - 11 answers
question4 : 11votes - 15 answers

So you would have to choose one main sorting criteria: either votes or answers

1 Answer

+2 votes
by
edited by
 
Best answer

There is no clean way to do this. You'll need to hack the core. But I suggest taking a harder approach and ADD more lines in order not to CHANGE so many (only one, actually).

Also bear in mind the core is not tuned to perform the query that you want to run so if there are too many questions for a given tag the query will run slower that the "most recent questions" one.

This will force the sorting for most votes and for questions with equal amounts of votes then the secondary sorting will be the amount of answers.

Follow these steps:

1. Open file qa-include/pages/tag.php

2. Locate these 4 lines:

list($questions, $tagword)=qa_db_select_with_pending(
    qa_db_tag_recent_qs_selectspec($userid, $tag, $start, false, qa_opt_if_loaded('page_size_tag_qs')),
    qa_db_tag_word_selectspec($tag)
);

3. Replace them with:

function qa_db_tag_most_votes_qs_selectspec($voteuserid, $tag, $start, $full=false, $count=null)
{
    $count=isset($count) ? min($count, QA_DB_RETRIEVE_QS_AS) : QA_DB_RETRIEVE_QS_AS;
    require_once QA_INCLUDE_DIR.'util/string.php';
    $selectspec=qa_db_posts_basic_selectspec($voteuserid, $full);
    $selectspec['source'].=" JOIN (SELECT postid FROM ^posttags WHERE wordid=(SELECT wordid FROM ^words WHERE word=$ AND word=$ COLLATE utf8_bin LIMIT 1)) y ON ^posts.postid=y.postid ORDER BY ^posts.netvotes DESC, ^posts.acount DESC LIMIT #,#";
    array_push($selectspec['arguments'], $tag, qa_strtolower($tag), $start, $count);
    return $selectspec;
}

list($questions, $tagword)=qa_db_select_with_pending(
    qa_db_tag_most_votes_qs_selectspec($userid, $tag, $start, false, qa_opt_if_loaded('page_size_tag_qs')),
    qa_db_tag_word_selectspec($tag)
);

For v1.6.x, you have to follow similar steps with some considerations.

1. Instead of qa-include/pages/tag.php you need to edit qa-include/qa-page-tag.php

3. Use the same code but change this line:

require_once QA_INCLUDE_DIR.'util/string.php';

with this other one:

require_once QA_INCLUDE_DIR.'qa-util-string.php';

by
edited by
Hi Pupi, its not working (my version is 1.6.2)
by
Why are you using 1.6.2 when the latest version is 1.7.1?
by
because I have done a lot of modifications everywhere and it will be a lots of time to edit everything again!
by
I've added a 1.6.x approximation. But bear in mind you should ALWAYS specify a Q2A version when asking. Particularly, if you are not using the Q2A version that is stable at the time you post your question
by
edited by
thanks Pupi (sorry for that), its working, BUT it sort only by note, but not answer too. For instance => http://odlg.org/Plante-Medicinale/fatigue+chronique - so 9/2 is before 9/5 - how could I choose this critera "answer" in your code?
by
Well, your questions included "(and/or most answers)", so I went for the "or" part.

You can't sort by 2 criteria at the same time but you can apply a secondary sorting criteria when there is more than one element that should be placed in the same position. That seems to be what you mean by 9/2 and 9/5.

I've updated the answer. Just replace the "$selectspec['source']..." line
...