zoukankan      html  css  js  c++  java
  • 对json排序

    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <script>
    //
        var willSort = [
        {
            'name':'a',
            'age':21,
            'height':'170',
            'date':'2014-10-12'
        },
        {
            'name':'b',
            'age':19,
            'height':'170',
            'date':'2014-10-13'
        },
        {
            'name':'c',
            'age':22,
            'height':'170',
            'date':'2014-10-14'
        },
        {
           'name':'d',
            'age':2,
            'height':'170',
            'date':'2014-10-15'
        },
        {
           'name':'e',
            'age':30,
            'height':'170',
            'date':'2014-10-16'
        }
    ];
        
        
    //alert(willSort[0].date)

    /*
        @function     JsonSort 对json排序
        @param        json     用来排序的json
        @param        key      排序的键值
    */
    function JsonSort(json,key){
        //console.log(json);
        for(var j=1,jl=json.length;j < jl;j++){
            var temp = json[j],
                val  = temp[key],
                i    = j-1;
            while(i >=0 && json[i][key]>val){
                json[i+1] = json[i];
                i = i-1;   
            }
            json[i+1] = temp;

        }
        //console.log(json);
        return json;
    }
    var json = JsonSort(willSort,'date');
    //console.log(json);
              // alert(json.length)

    for(var i=0;i<json.length;i++){
        alert(json[i].date)
    //var date=(json[i].date)
    //    function formatDate(now)   {     
    //    now=now.replace(//Date(/,"").replace(/000)//,"");
    //    now= new Date(parseInt((now*1000).toLocaleString().substr(0,24)));
    //    var year=now.getFullYear();     
    //    var month=jia0(now.getMonth()+1);     
    //    var date=jia0(now.getDate());     
    //    var hour=jia0(now.getHours());     
    //    var minute=jia0(now.getMinutes());     
    //    var second=jia0(now.getSeconds());     
    //    return year+"-"+month+"-"+date+"   "+hour+":"+minute+":"+second;     
    // }
    //function jia0(n){
    //    if(n<10)
    //    n="0"+n;
    //    return n;
    //}
    //var date="/Date(1410340800000)/";
    //document.write(formatDate(date));
    }



    </script>
    </head>

    <body>
    </body>
    </html>

  • 相关阅读:
    JAVA for(i = 0; i<a.length; i++) 解析
    3.2.2多维数组 3.3 排序
    3.2数组
    字符串和数组
    2.7.3与程序转移有关的跳转语句
    2.7.2 循环语句
    读书共享 Primer Plus C-part 4
    Linux 批量修改文件名
    关于/usr/local/lib/libz.a(zutil.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC解决办法
    做一个有深度的程序猿
  • 原文地址:https://www.cnblogs.com/lovedream/p/4191139.html
Copyright © 2011-2022 走看看