简体   繁体   English

我如何首先对一个值进行排序,而不对另一个进行排序?

[英]How can I sort first for a value, than for another?

This is my code : 这是我的代码

<div id="fascia-filtri-etichette">
    <div data-title="m" data-preorder="order-2" class="item">m</div>    
    <div data-title="2013" data-preorder="order-3" class="item">2013</div>    
    <div data-title="2012" data-preorder="order-3" class="item">2012</div>
    <div data-preorder="order-4" id="fascia-filtri-etichette-cancella">DELETE</div>
    <div data-title="E" data-preorder="order-1" class="item">E</div>
    <div data-title="S" data-preorder="order-1" class="item">S</div>
    <div data-title="2014" data-preorder="order-3" class="item">2014</div>
</div>

<script>
    var items = $("#fascia-filtri-etichette" + ' > div');
    items.sort(function (a, b) {
        return a.getAttribute('data-title') > b.getAttribute('data-title') || a.getAttribute('data-preorder') > b.getAttribute('data-preorder');
    }).detach().appendTo($("#fascia-filtri-etichette"));    
</script>

and I'd like to order (ascending) the divs first for the pre-order field, than (thus for each previous block) for title. 我想先对div排序(升序)作为pre-order字段,而不是(因此对每个前面的块)标题。

The result should be: 结果应为:

E // order 1
S // order 1
m // order 2
2012 // order 3
2013 // order 3
2014 // order 3
DELETE // order 4

Where am i wrong? 我哪里错了?

Your mistakes here are 你的错误是

  1. Sorting function should -1 or 0 or 1 , not true / false 排序功能应为-1或0或1,而不是true / false
  2. Code is messy and it is not easily checkable to get how it works. 代码混乱不堪,难以掌握其工作原理。 I didn't get how you combine title sorting and pre - order, and probably mistake is here 我不知道如何将标题排序和预购结合起来,这里可能有错误

it should look like this 它应该看起来像这样

items.sort(function (a, b) {
    var po = a.getAttribute('data-preorder').localeCompare(b.getAttribute('data-preorder'));
    if(po != 0) return po;

    return a.getAttribute('data-title').localeCompare( b.getAttribute('data-title'));
});

First it should go and check preorder, only then compare title. 首先,它应该去检查预订,然后再比较标题。 Here is working example http://jsfiddle.net/rczjm4ph/ 这是工作示例http://jsfiddle.net/rczjm4ph/

Use 2 sort functions instead of one. 使用2个排序函数而不是一个。 The testcase http://jsfiddle.net/a47xv32f/ 测试用例http://jsfiddle.net/a47xv32f/

var items = $("#fascia-filtri-etichette" + ' > div');
items.sort(function (a, b) {
    return a.getAttribute('data-title') > b.getAttribute('data-title');
}).sort(function (a, b) {
    return a.getAttribute('data-preorder') > b.getAttribute('data-preorder');
}).detach().appendTo($("#fascia-filtri-etichette"));    

It is still can be done with a single sort() : 仍然可以通过单个sort()来完成:

var items = $("#fascia-filtri-etichette" + ' > div');
items.sort(function (a, b) {
    var cmpOrder = a.getAttribute('data-preorder').localeCompare(b.getAttribute('data-preorder'));

    if (cmpOrder !== 0) return cmpOrder;

    return a.getAttribute('data-title').localeCompare(b.getAttribute('data-title'));
});    

Make a comparison function that looks at the pre-order first, then the title for items where pre-order are equal: 创建一个比较函数,该函数首先查看预购商品,然后查看预购商品相等的商品的标题:

items.sort(function (a, b) {
  var aPre = a.getAttribute('data-preorder');
  var bPre = b.getAttribute('data-preorder');
  if (aPre == bPre) {
    return a.getAttribute('data-title') > b.getAttribute('data-title') ? 1 : -1;
  } else {
    return aPre > bPre ? 1 : -1;
  }
}).detach().appendTo($("#fascia-filtri-etichette"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM