-53

Sometimes the topvoted answer is too long and complicated and many people want a 1-line solution to their problem. Currently there are 3 ways to sort the answers. It will be great if we can sort the answers by content length showing the shortest answers on top. What do you guys think about adding such a feature?

Edit: Seeing the downvotes and the total disagreement this was probably a bad idea(sounded cool in my mind)

16
  • 27
    We don't want short answers unless they are the best answers.
    – Paulie_D
    Feb 3, 2016 at 17:09
  • 6
    An answer being a short, 1-line solution does not necessarily mean it's a good and working solution that will actually help you. Honestly, and this is pure opinion from here on, I think the best way to learn from an answer is to understand the answer, and why that answer solves the problem. For that reason, I don't appreciate short, 1-line solutions unless they're extremely simple and only need a sentence or two of explanation.
    – Kendra
    Feb 3, 2016 at 17:11
  • @Paulie_D it will be another sort option just like active oldest and votes and not the default. maybe named short
    – Bosak
    Feb 3, 2016 at 17:11
  • 2
    What it would be isn't relevant here.
    – Paulie_D
    Feb 3, 2016 at 17:12
  • 2
    Well oldest and active don't give you the best answers either. It's just a sorting option. Giving more options to people isn't bad
    – Bosak
    Feb 3, 2016 at 17:16
  • 16
    TLDR people aka. the copy&paste generation. Feb 3, 2016 at 17:18
  • 5
    Oldest and active both have their uses. Example: You are at a company using old products, and cannot upgrade. You have an issue with your IDE, but it's an older version and finding an answer is hard. You find an old question on SO about your issue, asked around the time your IDE version was widely used. You see newer answers about newer versions, so you sort "oldest" to find answers more likely to be relevant to your version. Similarly with active, if you're using the newer version, you can use "active" to check for updated or newer answers, more likely to be addressing your version.
    – Kendra
    Feb 3, 2016 at 17:19
  • 4
    I could see using this the opposite way. You could make the longer answers first because you want to see the ones that have more detail. But even that has the same problem: long doesn't necessarily mean good either. The longest answers could just have rambling or way more code than necessary.
    – BSMP
    Feb 3, 2016 at 18:25
  • 3
    Cause, you know, some people don't actually want to learn anything, they just want to be able to copy-pasta some code from the internet without knowing what it does or what the limitations of said code are, mash it all together, and get paid for it... right?
    – user4639281
    Feb 3, 2016 at 18:42
  • I always want one-line answers. If it lays golden eggs and results in world peace, so much the better. Seriously though, this is a ... .[be nice].. less-than-useful idea:( Feb 3, 2016 at 19:00
  • 1
    @TinyGiant - you missed the important ' then run!' step from the end. Feb 3, 2016 at 19:02
  • Ah, the most important part. @MartinJames
    – user4639281
    Feb 3, 2016 at 19:10
  • 4
    This is how you get more link-only answers. Feb 3, 2016 at 19:12
  • @BilltheLizard Short link, link only answers. :)
    – Zizouz212
    Feb 3, 2016 at 21:59
  • 1
    Don't let the mob tell you it's a bad idea. You should be able to sort however you want. The down-voters don't need to use the sort option if they don't want to. Jun 2, 2020 at 10:12

1 Answer 1

7

I have a user script that sorts answers by the length of the first code block. It should be easy to make it sort by total answer length instead, if you want.

Actually, just replacing the selector pre:first on line 33 with .post-text should be enough. Of course, you may also wish to rename the script and maybe tweak the tooltip text, and change the @match line to make it run on SO instead of CodeGolf.SE.

Heck, let me just post the tweaked code here:

// ==UserScript==
// @name       Sort SO answers by length
// @namespace  http://vyznev.net/
// @version    0.1
// @description  Add a tab to sort answers by length on Stack Overflow
// @match      *://stackoverflow.com/questions/*
// @copyright  2016, Ilmari Karonen
// @grant      none
// ==/UserScript==

// based on Code Golf SE sorter v0.6 from https://gist.github.com/vyznev/9b55e1f96ee129d53466

var inject = function () {
    var $votestab = $('#tabs a[href*="?answertab=votes"]:first');
    if ( !$votestab.length ) return;

    var $golftab = $votestab.clone().attr( {
        href: function (i,v) { return v.replace( /[?&]answertab=votes\b/, '?answertab=shortest' ) },
        title: "Shortest answers first"
    } ).text('shortest').removeClass('youarehere').insertAfter($votestab);

    var tab = /[?&]answertab=([^&]*)/.exec( location.search );
    tab = (tab && tab[1]) || localStorage.getItem( 'codegolf_sort_tab' ) || 'default';
    localStorage.setItem( 'codegolf_sort_tab', tab );
    if ( tab != 'shortest' ) return;

    $('#tabs a.youarehere').removeClass('youarehere').addClass('youwerehere');
    $golftab.addClass('youarehere');
    $('<style type="text/css">#tabs a.youwerehere { border-bottom: 1px dotted #ea0 }</style>').appendTo('head');

    $('.answer').each( function () {
        var $this = $(this);
        var answerid = $this.data('answerid');
        if ( answerid ) $this.prepend( $this.prev('a[name="' + answerid + '"]') );
        var $code = $this.find('.post-text');
        var score = ($code.length ? $code.text().length : 1e9);
        $this.data( 'golfscore', score + Math.random() );
    } ).not('.deleted-answer, .downvoted-answer').sort( function (a,b) {
        return $(a).data('golfscore') - $(b).data('golfscore');
    } ).insertAfter('#answers-header');

    // scroll back to target post after re-sorting
    var m = /^#(?:(\d+)|comment(\d+)_\d+)$/.exec( location.hash );
    var id = m && ( m[1] ? "answer-" + m[1] : "comment-" + m[2] );
    if ( id ) document.getElementById( id ).scrollIntoView();
};

var scriptElem = document.createElement( 'script' );
scriptElem.textContent = "$(" + inject + ");";
document.body.appendChild( scriptElem );
2
  • Too much jQuery, otherwise it's cool
    – user4639281
    Feb 4, 2016 at 0:13
  • 1
    +1. If you could rewrite this to one line, this should be the accepted answer.
    – Chris
    Jun 1, 2017 at 8:17

You must log in to answer this question.

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