Return to Snippet

Revision: 8646
at October 2, 2008 05:02 by kouphax


Initial Code
var DOMPositionComparator = function(a,b){
	if(Prototype.Browser.IE){
		return a.sourceIndex - b.sourceIndex;
	}else if(Prototype.Browser.Gecko){
		return 3 - (a.compareDocumentPosition(b) & 6);
	}
};

var sortedFields = $$('img.L,img.M').sort(DOMPositionComparator);

Initial URL

                                

Initial Description
This function will sort an array of DOM elements in order of the DOM position.  Useful when using calls such as $$("img.L,img.M") which doesn;t return a sorted element array.

It uses prototype but you can replace the Prototype.Browser stuff with anything else.

Initial Title
Sort Element Array by DOM Position

Initial Tags
sort, array, DOM

Initial Language
JavaScript