User:Seb35/sortValues.js

From Wikidata
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// Sort the values of P348 (versions of a software)
// Authors: Seb35, Od1n - licence: CC-BY-SA 3.0
$( function($) {
	
	var $container = $('#P348');
	if (!$container.length) return;
	
	var $parent = $container.find('.wikibase-statementlistview-listview'),
	    $children = $parent.find('.wikibase-statementview');
	
	$children = $( $children.toArray().sort( function( a, b ) {
		
		var ta = a.querySelector('.wikibase-snakview-value').textContent, la = ta.split('.'), ca,
		    tb = b.querySelector('.wikibase-snakview-value').textContent, lb = tb.split('.'), cb;
		while( la.length && lb.length ) {
			ca = Number(la.shift());
			cb = Number(lb.shift());
			if( isNaN(ca) || isNaN(cb) ) break;
			if( ca > cb )
				return 1;
			else if( ca < cb )
				return -1;
		}
		if( la.length || lb.length )
			return la.length - lb.length;
		if( ta > tb )
			return 1;
		else if( ta < tb )
			return -1;
		return 0;
	} ) );
	
	$children.detach().appendTo($parent);
} );