I have a script, the goal of which is to sort a list of entire div classes (containing images) in ascending order as determined by a number within that div class. It has to work at the push of a button. It works relatively well, except it keeps sorting the items by the first digit rather than the number as a whole (resulting in an arrangement like 1, 10, 11, 2, 3, etc.)
My entire code is below, please show me how to fix this.
NOTE: I am looking for any possible solution but if it's possible to keep the script close to its current form I would prefer that
CSS:
.number{
display:block;
color:black;
font-size:30px;
margin:20px
}
HTML:
<div id="sortingbutton">
<button>Sorting Button</button>
</div>
</div>
<ul id="list">
<li>
<div class="number">5</div>
<img src="http://i.imgur.com/b70bLk1.png" width="250px" height="250px" alt="aqua" />
</li>
<li>
<div class="number">4</div>
<img src="http://i.imgur.com/keE5Thi.png" width="250px" height="250px" alt="tan" />
</li>
<li>
<div class="number">1</div>
<img src="http://i.imgur.com/EgqCTZJ.png" width="250px" height="250px" alt="black" />
</li>
<li>
<div class="number">3</div>
<img src="http://i.imgur.com/4onkGsz.png" width="250px" height="250px" alt="grey" />
</li>
<li>
<div class="number">9</div>
<img src="http://i.imgur.com/To88ZFN.png" width="250px" height="250px" alt="orange" />
</li>
<li>
<div class="number">2</div>
<img src="http://i.imgur.com/6ZtB1VW.png" width="250px" height="250px" alt="purple" />
</li>
<li>
<div class="number">6</div>
<img src="http://i.imgur.com/5Pz2Q2Y.png" width="250px" height="250px" alt="yellow" />
</li>
<li>
<div class="number">7</div>
<img src="http://i.imgur.com/VqAdV1K.png" width="250px" height="250px" alt="blue" />
</li>
<li>
<div class="number">8</div>
<img src="http://i.imgur.com/4HCxTpm.png" width="250px" height="250px" alt="green" />
</li>
<li>
<div class="number">10</div>
<img src="http://i.imgur.com/JjjHmM0.png" width="250px" height="250px" alt="pink" />
</li>
<li>
<div class="number">11</div>
<img src="http://i.imgur.com/EpB8YgU.png" width="250px" height="250px" alt="Red" />
</li>
</ul>
Javascript:
window.onload = function () {
var desc = false;
document.getElementById("sortingbutton").onclick = function () {
sortUnorderedList("list", desc);
desc = !desc;
return false;
}
}
function compareText(a1, a2) {
var t1 = a1.innerText,
t2 = a2.innerText;
return t1 > t2 ? 1 : (t1 < t2 ? -1 : 0);
}
function sortUnorderedList(ul, sortDescending) {
if (typeof ul == "string") {
ul = document.getElementById(ul);
}
var lis = ul.getElementsByTagName("LI");
var vals = [];
for (var i = 0, l = lis.length; i < l; i++) {
vals.push(lis[i]);
}
vals.sort(compareText);
if (sortDescending) {
vals.reverse();
}
ul.innerHTML = '';
for (var i = 0, l = vals.length; i < l; i++) {
ul.appendChild(vals[i]);
}
}
Why not just use a1.innerText - a2.innerText
in compareText
?
Explanation
When you compare two strings using less and greater than symbols javascript doesn't convert them to integers, as javascript can compare two strings, no problem. For example "10" > "2"
returns true
, cause "10"
is longer.
However when you use subtraction operator javascript can't subtract strings so it converts it into integers.
Use parseInt to convert string variables into integers.
http://www.w3schools.com/jsref/jsref_parseint.asp
Sorting by first digit is string sorting.
for (var i = 0, l = lis.length; i < l; i++) {
vals.push(parseInt(lis[i], 10));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.