javascript sort 排序
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="robots" content="all" />
<style type="text/css">
</style>
<script type="text/javascript" src="jquery-1.7.1.js"></script>
</head>
<body>
<div id="sort">
点击排序</div>
<ul>
<li>
<p>
2</p>
</li>
<li>
<p>
1</p>
</li>
<li>
<p>
3</p>
</li>
<li>
<p>
5</p>
</li>
<li>
<p>
9</p>
</li>
<li>
<p>
6</p>
</li>
<li>
<p>
4</p>
</li>
<li>
<p>
8</p>
</li>
<li>
<p>
7</p>
</li>
<li>
<p>
10</p>
</li>
</ul>
<script type="text/javascript">
var temp = new Array();
$("#sort").click(function () {
var container = $("ul");
var rows = container.find("li");
if ($(this).hasClass("selected")) {
temp.reverse();
} else {
$.each(rows, function (i) {
temp[i] = rows.eq(i);
});
temp.sort(sortbyp);
}
function sortbyp(a, b) {
var Aval = parseInt(a.find("p").text());
var Bval = parseInt(b.find("p").text());
if (Aval > Bval) {
return -1;
} else {
return 1;
}
}
container.empty();
$.each(temp, function (i) {
container.append(temp[i]);
});
$(this).addClass("selected");
});
</script>
</body>
</html>