Take the 2-minute tour ×
Stack Apps is a question and answer site for apps, scripts, and development with the Stack Exchange API. It's 100% free, no registration required.

Looking at this stackoverflow question:

http://stackoverflow.com/questions/3012488?tab=votes#tab-top

when sort is by votes, the accepted_answer is always on top regardless of answer's upvotes.

Calling questions/id/answers with sort=votes does not give the same result.
Here an example:

http://api.cooking.stackexchange.com/1.0/questions/567/answers?sort=votes&body=true

or, for a formatted view:

http://stackprinter.appspot.com/export?question=567&format=HTML&service=cooking.stackexchange&printer=false

Is it correct?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Yes, you are explicitly telling it to sort on votes and it dutifully does so.

Drop the sort (or specify the default, activity) and the accepted answer will be listed at the top. see kevin's comments

Which brings up another interesting question, which I remember being discussed elsewhere but can't be bothered to search: how answers are shuffled around in the default sort.

If you have a monster question with 100's of answers and you use the default sort, which is somewhat random, no?, and page through, it seems that you are going to probably get unpredictable results.

So, the only way to reliably page through answers is to specify a non-default sort, which means that the accepted answer is not going to be pinned to the top of the list.

I don't see a simple way around this except to pull the accepted answer and manually add it to the head of the first page of results and omit it when encountered later in the paged operation.

but that is neither here nor there.

use accepted_answer_id to prefetch the accepted answer and insert it into the head of your list and ignore it if/when it comes around in results.

share|improve this answer
    
The default sort for /questions/{id}/answers is activity. There is no guarantee that the accepted answer is the first returned one, it just so happens that the best answer tends to be the most active one. –  Kevin Montrose Jul 22 '10 at 16:18
    
I'd also point out that there are vanishingly view questions (<100 glancing at a recent SO backup) with >100 answers, most of which are quite old. We've been strongly discouraging their creation for awhile. –  Kevin Montrose Jul 22 '10 at 16:22
    
@kevin - thanks for the clarification. with those numbers i would agree that the possible unpredictability of large answer sets represent an edge that doesn't warrant special treatment. –  Sky Sanders Jul 22 '10 at 16:46
    
@code@Kevin Why stackoverflow sorting by votes put the accepted answer on top? This time i hoped for a by-design feature but unluckily it is not like that :-). –  systempuntoout Jul 23 '10 at 7:13
    
@system - the sites do not consume the api, they have direct access to the data and projecting the rules/constraints that we follow to the sites in order to understand the api is an exercise in futility. Sorry. About '@' replying: only the first @ is honored, the rest are ignored, so in this case, I am going to get the comment no matter because it is my post, but kevin needs an @kevin to get it. So you could say 'hey code and @kevin,..' or even '@kevin, what about @code's foo?' etc. Notice that there is only one replyToUser on comments? –  Sky Sanders Jul 23 '10 at 8:04
    
@code i'm well aware that sites do not consume the api. Looking at SE sites behaviour on votes ordering, i simply thought that an accepted answer had more value\votes than the other answers regardless of number of votes; so i expected to see it reflected in the API.That's it. Thanks for the hints for the @Kevin comment ;). –  systempuntoout Jul 23 '10 at 12:38
    
@system - i think they do something similar to what I describe, probably in the sql. that's where it would be most efficient. Maybe kevin knows, and will tell us just for edification. –  Sky Sanders Jul 23 '10 at 12:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .