排序元素

为了有效地对元素进行排序(一次性完成并且 DOM 中断最少),我们需要:

  1. 找到元素
  2. **** 根据设定条件排序
  3. 回 DOM
<ul id='my-color-list'>
    <li class="disabled">Red</li>
    <li>Green</li>
    <li class="disabled">Purple</li>
    <li>Orange</li>
</ul>
  1. 找到他们 - .children().find()

    这将为我们提供一个类似数组的对象。

    var $myColorList = $('#my-color-list');
    
    // Elements one layer deep get .children(), any deeper go with .find()
    var $colors = $myColorList.children('li');
    
  2. 重新安排他们 - Array.prototype.sort()

    目前,这是根据 HTML 内容(也称为其颜色)以升序顺序返回元素。

    /**
     * Bind $colors to the sort method so we don't have to travel up
     * all these properties more than once.
     */
    var sortList = Array.prototype.sort.bind($colors);
    
    sortList(function ( a, b ) {
    
        // Cache inner content from the first element (a) and the next sibling (b)
        var aText = a.innerHTML;
        var bText = b.innerHTML;
    
        // Returning -1 will place element `a` before element `b`
        if ( aText < bText ) {
            return -1;
        }
    
        // Returning 1 will do the opposite
        if ( aText > bText ) {
            return 1;
        }
    
        // Returning 0 leaves them as-is
        return 0;
    });
    
  3. 插入它们 - .append()

    请注意,我们不需要首先分离元素 - append() 将移动已存在于 DOM 中的元素,因此我们不会有额外的副本

    // Put it right back where we got it
    $myColorList.append($colors);
    

让它可爱

添加排序按钮

<!-- previous HTML above -->
<button type='button' id='btn-sort'>
    Sort
</button>

设置排序方向的初始值

var ascending = true;

将我们的 DOM 元素和 sortList() 缓存到此处以最小化我们的 DOM 处理

var $myColorList = $('#my-color-list');
var $colors = $myColorList.children('li');
var sortList = Array.prototype.sort.bind($colors);

doSort() 功能中包装所有内容

// Put the sortList() and detach/append calls in this portable little thing
var doSort = function ( ascending ) {

    sortList(function ( a, b ) {
        // ...
    });

    $myColorList.append($colors);
};

$('#btn-sort') 添加点击处理程序

$('#btn-sort').on('click', function () {
    // Run the sort and pass in the direction value
    doSort(ascending);

    // Toggle direction and save
    ascending = !ascending;
});

现在都在一起了

var ascending = true;

var $myColorList = $('#my-color-list');
var $colors = $myColorList.children('li');
var sortList = Array.prototype.sort.bind($colors);

var doSort = function ( ascending ) {
    
    sortList(function ( a, b ) {

        var aText = a.innerHTML;
        var bText = b.innerHTML;

        if ( aText < bText ) {
            return ascending ? -1 : 1;
        }

        if ( aText > bText ) {
            return ascending ? 1 : -1;
        }

        return 0;
    });
    
    $myColorList.append($colors);

};

$('#btn-sort').on('click', function () {
    doSort(ascending);
    ascending = !ascending;
});

奖金

多级排序(对已排序的元素进行分组)

// ...

var doSort = function ( ascending ) {

    sortList(function ( a, b ) {

        // ...initial sorting...

    }).sort(function ( a, b ) {
        
        // We take the returned items and sort them once more
        var aClass = a.className;
        var bClass = b.className;
        
        // Let's group the disabled items together and put them at the end

        /**
         * If the two elements being compared have the same class
         * then there's no need to move anything.
         */
        if ( aClass !== bClass ) {
            return aClass === 'disabled' ? 1 : -1;
        }
        return 0;
    });

    // And finalize with re-insert
    $myColorList.append($colors);
};

// ...

你能更进一步吗?

添加另一个按钮以切换禁用的组排序

MDN - Array.prototype.sort()