7

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.

8

3 Answers 3

8

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
  • 2
    makyen-is-sorted ... so true ...
    – rene Mod
    Commented Dec 16, 2020 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.
    Commented Dec 16, 2020 at 14:26
10

If you do want some kind of sorting, you'll have to write a custom cross-site SQL query in the Stack Exchange Data Explorer (and accept the fact that it's updated only once a week, on Sunday morning). And a simple search on some text in the post body will probably time out due to the large volume of Stack Overflow.

1
1

This is far from perfect, but I typically use the search modifier created when I want to see the most recent results. (I do not get results sorted from the newest to oldest. But quite often I am in situation when I want to see results which are not too old - for example, if I am trying to see whether some topic was discussed recently and I want to search over all per-site-metas.)

For example, I could search for banana pancakes created:2022-01-01... (In this way I get only the posts created since 2022.)

And if I want older results, I can try banana pancakes created:2021-01-01..2022-01-01, etc.

1
  • Awesome! This is the closest what I wanted. Thanks :)
    – Neeraj
    Commented Apr 3, 2023 at 12:46

You must log in to answer this question.

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