jquery reorder elements

Solutions on MaxInterview for jquery reorder elements by the best coders in the world

showing results for - "jquery reorder elements"
Klara
30 Nov 2018
1/*
2FROM:
3```
4<ul>
5      <li>2</li>
6      <li>10</li>
7      <li>5</li>
8      <li>7</li>
9      <li>6</li>
10      <li>8</li>
11      <li>9</li>
12      <li>4</li>
13      <li>1</li>
14      <li>3</li>
15</ul>
16```
17
18TO:
19```
20<ul>
21   <li>1</li>
22   <li>3</li>
23   <li>6</li>
24   <li>7</li>
25   <li>2</li>
26   <li>10</li>
27   <li>5</li>
28   <li>8</li>
29   <li>9</li>
30   <li>4</li>
31</ul>
32```
33*/
34
35var special = $("li").filter(isSpecial).sort(byText).prependTo("ul")
36
37function isSpecial() {
38  var v = parseInt($(this).text())
39  return v === 1 || v === 3 || v === 6 || v === 7
40}
41
42function byText(a, b) {
43  return $(a).text().localeCompare($(b).text())
44}