Elementals.js

Latest Version: 3.7 Final 20 December 2018

Method _.USort.element

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:
_.USort.element(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

_.USort.element(
	_d.getElementByID('test'),
	function(a, b) {
		return _.compare.string(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

Advertisement