Elementals.js

Latest Version: 2.0 RC 3 14 March 2017

Method _.uSortElement

Summary:

Allows you to sort a list or table body on the DOM using a callback function. The contents of each row or list item.

If you pass this a variable that's not a DOM Element of type UL, OL, or TBODY an appropriate TypeError will be thrown.

Calling Convention:
_.uSortElement(Element, callback)
Parameters:
Element
A TBODY, UL, or OL type Element object.
callback

The callback function used to handle the sort. This calls a normal Array.sort where the structure of that Array is as follows:

If the passed Element is a UL or OL the Array consists of objects containing Object.tag, a reference to the LI's DOM element, and Object.data the plain-text contents of that Element, as per _.nodeText.

When working with a TBODY the Array again consists of objects where Object.tag points at the TR's DOM element. The Object.data on the other hand is an Array where each element is the plain-text contents represents each TH or TD in that row.

Returns:
Nothing

Example

HTML

<ul id="test">
	<li>First</li>
	<li>Second</li>
	<li>Third</li>
	<li>Fourth</li>
	<li>Fifth</li>
	<li>Sixth</li>
</ul>

JavaScript

_.uSortElement(
	_d.getElementByID('test'),
	function(a, b) {
		return _.compareString(a.data, b.data);
	}
);

Output

• Fifth
• First
• Fourth
• Second
• Sixth
• Third

Live Examples

These demo's are also included in the distribution archive.

  1. Sorting a UL or OL
  2. Sorting a table body