Return to Snippet

Revision: 19610
at October 27, 2009 08:45 by roryok


Initial Code
javascript:(function(){function%20f(r){var%20t=r.getElementsByTagName("span");var%20i;for(i=0;i<t.length;i++){if(t[i].hasAttribute("email")){return%20t[i].innerHTML}}return%20""}function%20ds(a,b){a=a[0].toLowerCase();b=b[0].toLowerCase();if(a<b)return%20-1;if(a>b)return%201;return%200}function%20sl(m){var%20l=Array.prototype.slice.call(m.getElementsByTagName("tr"),0);if(!l.length)return;var%20b=l[0].parentNode;var%20s=[];for(var%20i=0;i<l.length;i++){s[i]=[f(l[i]),l[i]]}s=s.sort(ds);for(i=0;i<s.length;i++){b.appendChild(s[i][1])}}var%20m=document.getElementById('canvas_frame').contentWindow.document.getElementById(":on");if(m)sl(m)})()


// Expanded Script

javascript:(function(){

function findFrom(r) {

    var span = r.getElementsByTagName("span");
    var i;
    for (i = 0; i < span.length; i++) {
        if (span[i].hasAttribute("email")) {
            return span[i].innerHTML;
        }
    }
    return ""; 
   
}
function doSort(a,b)
{
	a = a[0].toLowerCase();
	b = b[0].toLowerCase();
	if(a<b) return -1;
	if(a>b) return 1;
	return 0;
}
function sortList(m)
{
	var list = Array.prototype.slice.call(m.getElementsByTagName("tr"), 0);
	if(!list.length) return;
	var tbody = list[0].parentNode;
	var sortList = [];
	for(var i=0; i<list.length; i++)
	{
		sortList[i] = [findFrom(list[i]),list[i]];
	}
	sortList = sortList.sort(doSort);
	for(i=0; i<sortList.length; i++)
	{
		tbody.appendChild(sortList[i][1]);
	}
}

var messages = document.getElementById('canvas_frame').contentWindow.document.getElementById(":on");
if(messages) sortList(messages);


})()

Initial URL

                                

Initial Description
This is based on the [Sort Gmail Messages By Bookmarklet](http://snipplr.com/view/10432/sort-gmail-messages-by-subject/ "Sort Gmail Messages By Bookmarklet - http://snipplr.com/view/10432/sort-gmail-messages-by-subject/")  snippet.


Drag this into your Bookmarks: [SortGmailBySender](javascript:&#40;function&#40;&#41;&#123;function%20f&#40;r&#41;&#123;var%20t=r.getElementsByTagName&#40;&#34;span&#34;&#41;;var%20i;for&#40;i=0;i<t.length;i++&#41;&#123;if&#40;t[i].hasAttribute&#40;&#34;email&#34;&#41;&#41;&#123;return%20t[i].innerHTML&#125;&#125;return%20&#34;&#34;&#125;function%20ds&#40;a,b&#41;&#123;a=a[0].toLowerCase&#40;&#41;;b=b[0].toLowerCase&#40;&#41;;if&#40;a<b&#41;return%20-1;if&#40;a>b&#41;return%201;return%200&#125;function%20sl&#40;m&#41;&#123;var%20l=Array.prototype.slice.call&#40;m.getElementsByTagName&#40;&#34;tr&#34;&#41;,0&#41;;if&#40;!l.length&#41;return;var%20b=l[0].parentNode;var%20s=[];for&#40;var%20i=0;i<l.length;i++&#41;&#123;s[i]=[f&#40;l[i]&#41;,l[i]]&#125;s=s.sort&#40;ds&#41;;for&#40;i=0;i<s.length;i++&#41;&#123;b.appendChild&#40;s[i][1]&#41;&#125;&#125;var%20m=document.getElementById&#40;'canvas_frame'&#41;.contentWindow.document.getElementById&#40;&#34;:on&#34;&#41;;if&#40;m&#41;sl&#40;m&#41;&#125;&#41;&#40;&#41; "SortGmailBySender Bookmarklet")


A few caveats of this snippet (and the original)

1. This will screw up your checkbox selection order. Shift clicking to select multiple messages will no longer work until you refresh the view
2. If you archive or delete messages in this view, they will not disappear until you refresh gmail, but remember you can do a much lighter refresh by simply hitting the internal gmail 'refresh' link, rather than F5. 
3. I had to change the table selector from ":op" in the original to ":on" in the new version. gmail code changes regularly so you may have to tweak code a little.

Initial Title
Sort Gmail Messages By Sender

Initial Tags
javascript, sort

Initial Language
JavaScript