如何仅使用 Jquery 对 div 列表进行排序?



我有一个包含很多元素的div列表,我想做很多排序:

<div id="lista-terremoti" style="">
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536017841000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;23:37:21 UTC</div>
<div class="eq-lista-luogo">Al largo dell'Ecuador</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">4.9 M</div>
<div class="eq-lista-ipo">40 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536017747000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;23:35:47 UTC</div>
<div class="eq-lista-luogo">Isole Marianne</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">5 M</div>
<div class="eq-lista-ipo">200 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536015211000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;22:53:31 UTC</div>
<div class="eq-lista-luogo">California centrale, Stati Uniti</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">2.5 M</div>
<div class="eq-lista-ipo">15 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536015088000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;22:51:28 UTC</div>
<div class="eq-lista-luogo">Alaska meridionale</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">2.1 M</div>
<div class="eq-lista-ipo">74.6 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
</div>

每个div.eq-lista都是一场地震,其中包含许多数据:UTC 日期(地震时间戳在.mark-eq-lista > span内(、地点震级深度

我想以两种方式对列表进行排序:

1(从最旧到最新排序(与现在相反(

2(从最高到最低(.eq-lista-magn(排序

我尝试了这段代码,但不起作用:

var items = $('#lista-terremoti > .eq-lista').get();
items.sort(function(a, b) {
var keyA = $(a).children(':first').children();
var keyB = $(a).children(':first').children();
if (keyA < keyB) return -1;
if (keyA > keyB) return 1;
return 0;
});
var div_list = $('#lista-terremoti');
$.each(items, function(i, quake) {
div_list.append(quake); /* This removes li from the old spot and moves it */
});

希望你能帮到我...多谢!

我会将时间戳的获取和幅度的获取抽象为单独的函数,以保持代码干燥。然后,您可以按

items.sort((a, b) => (
eqToTimestamp(a) - eqToTimestamp(b)
|| eqToMagn(a) - eqToMagn(b)
));

请注意,时间戳比较通常会达到 0 以外的值,在这种情况下,量级差异很少会对列表顺序产生影响。

const eqToTimestamp = eq => Number(eq.children[0].textContent);
const eqToMagn = eq => (
Number(eq.querySelector('.eq-lista-magn').textContent.match(/S+/))
);
const items = $('#lista-terremoti > .eq-lista').get();
items.sort((a, b) => (
eqToTimestamp(a) - eqToTimestamp(b)
|| eqToMagn(b) - eqToMagn(a)
));
const div_list = $('#lista-terremoti');
items.forEach(item => div_list.append(item));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="lista-terremoti" style="">
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536017841000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;23:37:21 UTC</div>
<div class="eq-lista-luogo">Al largo dell'Ecuador</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">4.9 M</div>
<div class="eq-lista-ipo">40 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536017747000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;23:35:47 UTC</div>
<div class="eq-lista-luogo">Isole Marianne</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">5 M</div>
<div class="eq-lista-ipo">200 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536015211000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;22:53:31 UTC</div>
<div class="eq-lista-luogo">California centrale, Stati Uniti</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">2.5 M</div>
<div class="eq-lista-ipo">15 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536015088000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;22:51:28 UTC</div>
<div class="eq-lista-luogo">Alaska meridionale</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">2.1 M</div>
<div class="eq-lista-ipo">74.6 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
</div>

要仅按量级排序,请使用items.sort((a, b) => eqToMagn(a) - eqToMagn(b));

const eqToTimestamp = eq => Number(eq.children[0].textContent);
const eqToMagn = eq => (
Number(eq.querySelector('.eq-lista-magn').textContent.match(/S+/))
);
const items = $('#lista-terremoti > .eq-lista').get();
items.sort((a, b) => eqToMagn(b) - eqToMagn(a));
const div_list = $('#lista-terremoti');
items.forEach(item => div_list.append(item));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="lista-terremoti" style="">
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536017841000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;23:37:21 UTC</div>
<div class="eq-lista-luogo">Al largo dell'Ecuador</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">4.9 M</div>
<div class="eq-lista-ipo">40 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536017747000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;23:35:47 UTC</div>
<div class="eq-lista-luogo">Isole Marianne</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">5 M</div>
<div class="eq-lista-ipo">200 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536015211000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;22:53:31 UTC</div>
<div class="eq-lista-luogo">California centrale, Stati Uniti</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">2.5 M</div>
<div class="eq-lista-ipo">15 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
<div class="eq-lista">
<div class="mark-eq-lista"><span style="display:none">1536015088000</span></div>
<div class="dati-eq">
<div class="eq-lista-row" style="">
<div class="eq-lista-data" style="">2018-09-03&nbsp;&nbsp;22:51:28 UTC</div>
<div class="eq-lista-luogo">Alaska meridionale</div>
<div id="magn-ipo-link">
<div class="eq-lista-magn">2.1 M</div>
<div class="eq-lista-ipo">74.6 Km</div>
<div class="eq-lista-link"><a href="#" rel="nofollow">Dettagli</a></div>
</div>
</div>
<div class="ulteriore" style="display:none"></div>
</div>
</div>
</div>

相关内容

  • 没有找到相关文章

最新更新