如何按字母顺序排序div?

时间:2015-02-12 20:28:42

标签: javascript jquery sorting

以下代码按字母顺序对面板标题的第一个单词进行排序,但我希望它能够扩展为对标题的其余单词进行排序(而不仅仅是第一个单词)。我怎么做到这一点?谢谢

 <!--HTML-->
<div class='col-md-3'>    
  <div class='panel panel-default drop-shadow'>
    <div class="panel-heading text-center">
      <h3 class="panel-title"><a href="#">ABC 123</a></h3>
    </div>

    <div class='panel-body text-center'>
      <p class="desc">Lorem Ipsum</p>
    </div>

    <div class="panel-footer text-center">
     Footer verbiage here
    </div>

  </div>
</div>

<div class='col-md-3'>    
  <div class='panel panel-default drop-shadow'>
    <div class="panel-heading text-center">
      <h3 class="panel-title"><a href="#">ABC 456</a></h3>
    </div>

    <div class='panel-body text-center'>
      <p class="desc">Lorem Ipsum</p>
    </div>

    <div class="panel-footer text-center">
     Footer verbiage here
    </div>

  </div>
</div>


//JS - Alpha Sort ASC
$('#alphaLinkAsc').on('click', function () {
    var alphabeticallyOrderedDivs = $divs.sort(function (a, b) {
        return $(a).find(".panel-title").text() > $(b).find(".panel-title").text();
    });
    $(".filterList").html(alphabeticallyOrderedDivs);
});

1 个答案:

答案 0 :(得分:0)

sort()只能应用于数组。

您需要确保在排序方法起作用之前将所有div都放入数组中。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

$div在哪里被分配?你能确认它的类型是Array吗?