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 );
active
oldest
andvotes
and not the default. maybe namedshort
oldest
andactive
don't give you the best answers either. It's just a sorting option. Giving more options to people isn't bad