G
G
Gera112020-05-15 17:15:44
JavaScript
Gera11, 2020-05-15 17:15:44

How to sort by values ​​inside list items?

There is a list like:

<ul>
  <li>Книг <span>15</span></li>
  <li>Журналов <span>5</span></li>
  <li>Альбомов <span>25</span></li>
</ul>

How to make it so that the item with the largest number in the span is displayed in the first place in the list?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2020-05-15
@Gera11

$('li').sort((a, b) => $('span', b).text() - $('span', a).text()).appendTo('ul');

N
nokimaro, 2020-05-15
@nokimaro

If the list is derived from a PHP array, then use the function to sort the associative array by key https://qna.habr.com/answer?answer_id=1544113#answ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question