• 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>

  • 相关阅读:
    [算法]位运算问题之二
    [算法]位运算问题之一
    [算法]海量数据问题之二
    [算法]海量数据问题之一
    [算法]旋转词问题
    [算法]去掉字符串中连续出现的k个0子串
    [算法]字符串中数字子串的求和
    [算法]字符串之变形词问题
    Linux常用命令
    数据库中的事物
  • 原文地址:https://www.cnblogs.com/lzf0514/p/2501974.html
Copyright © 2020-2023  润新知