2

How can I sort questions that are returned to queries on the overall Stack Exchange search (so not a meta site, nor a main site like Stack Overflow), by date?

For example, how could I sort the questions that are returned on the query: banana pancakes by:

  1. Date of asking the question
  2. Date of accepting the answer
  3. Date of last answer
  4. Date of last modification

Most answers on meta related to search appear to refer to a filtering options (for dates), e.g. constraining a search to a range of dates, however, the resulting questions to such a query are not (necessarily) sorted on date.

6
4

There's currently no system supported way to sort search results for the all-Stack Exchange search. However, you can do so manually within the currently displayed page by executing some JavaScript.

Personally, I like to see lots of results on a single page, so I normally manually set the number of responses per page to 100 (the maximum), by adding &pagesize=100 to the URL. Given that the tab=newest parameter you provided on your example search URL does nothing, that would give a URL for your example search of https://stackexchange.com/search?q=banana%20pancakes&pagesize=100.

I keep the JavaScript which I use to sort a page of results by date in a bookmarklet. That code is:

javascript:void (function() {
    function getEpochFromDateInTitle(el) {
        return (new Date($(el).find('.relativetime')[0].title.replace(/ /, 'T'))).valueOf();
    }
    const searchResults = $('.search-results').addClass('makyen-is-sorted').toggleClass('makyen-sort-order');
    const sortOrder = searchResults.hasClass('makyen-sort-order') ? 1 : -1;
    searchResults.prepend($('.search-results > .question').toArray().sort((a, b) => (sortOrder * (getEpochFromDateInTitle(b) - getEpochFromDateInTitle(a)))));
})()

Having the code in a bookmarklet allows me to sort the search results with just a click. The above code is written such that executing the code again will reverse the sort order, so you can toggle back and forth between newest first and oldest first.

2
  • makyen-is-sorted ... so true ... – rene Dec 16 '20 at 7:34
  • Thank you! As the wiki in your answer says: Installation of a bookmarklet is performed by creating a new bookmark, and pasting the code into the URL destination field. So in particular for firefox I had to show the bookmarks bar > MB on Bookmarks Toolbar > Create a new bookmark > Copy your Javascript code in the Location field of the bookmark > Open the banana pancakes search on Stack Exchange:stackexchange.com/search?q=banana%20pancakes&pagesize=100 > click on the newly created bookmark. That indeed sorted the questions on answered or asked date. – a.t. Dec 16 '20 at 14:26

You must log in to answer this question.

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