作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含很多元素的 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 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 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 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 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 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 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 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 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 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 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 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 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>
关于javascript - 如何仅使用 Jquery 对 div 列表进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52157351/
我是一名优秀的程序员,十分优秀!