最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

javascript對(duì)JSON數(shù)據(jù)排序的3個(gè)例子

 更新時(shí)間:2014年04月12日 12:04:39   投稿:junjie  
這篇文章主要介紹了javascript對(duì)JSON數(shù)據(jù)排序的3個(gè)例子的相關(guān)資料

一、適用于數(shù)字排序和字幕排序
json 的排序方法有很多種,這是其中最簡(jiǎn)單的一種方法。

復(fù)制代碼 代碼如下:

var sortBy = function (filed, rev, primer) {
    rev = (rev) ? -1 : 1;
    return function (a, b) {
        a = a[filed];
        b = b[filed];
        if (typeof (primer) != 'undefined') {
            a = primer(a);
            b = primer(b);
        }
        if (a < b) { return rev * -1; }
        if (a > b) { return rev * 1; }
        return 1;
    }
};
var obj = [
    {b: '3', c: 'c'},
    {b: '1', c: 'a'},
    {b: '2', c: 'b'}
];

1、數(shù)字排序
復(fù)制代碼 代碼如下:
obj.sort(sortBy('b', false, parseInt));
console.log(obj);

2、字符串排序
復(fù)制代碼 代碼如下:
obj.sort(sortBy('b', false, String));
console.log(obj);


二、JSON排序例子2

復(fù)制代碼 代碼如下:

var willSort = [
    {
        name:'shangwenhe',
        age:25,
        height:170
    },
    {
        name:'zhangsan',
        age:31,
        height:169
    },
    {
        name:'lisi',
        age:31,
        height:167
    },
    {
        name:'zhaowu',
        age:22,
        height:160
    },
    {
        name:'wangliu',
        age:23,
        height:159
    }
];


/*
    @function     JsonSort 對(duì)json排序
    @param        json     用來(lái)排序的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,'age');
console.log(json);

三、JSON排序例子3

復(fù)制代碼 代碼如下:

var people = [
{
    name: 'a75',
    item1: false,
    item2: false
},
{
    name: 'z32',
    item1: true,
    item2: false
},
{
    name: 'e77',
    item1: false,
    item2: false
}];

function sortByKey(array, key) {
    return array.sort(function(a, b) {
        var x = a[key]; var y = b[key];
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    });
}

people = sortByKey(people, 'name');

PS:關(guān)于json操作,這里再為大家推薦幾款比較實(shí)用的json在線工具供大家參考使用:

在線JSON代碼檢驗(yàn)、檢驗(yàn)、美化、格式化工具:
http://tools.jb51.net/code/json

JSON在線格式化工具:
http://tools.jb51.net/code/jsonformat

在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson

json代碼在線格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.jb51.net/code/jsoncodeformat

在線json壓縮/轉(zhuǎn)義工具:

http://tools.jb51.net/code/json_yasuo_trans

C語(yǔ)言風(fēng)格/HTML/CSS/json代碼格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json

相關(guān)文章

最新評(píng)論

米易县| 通渭县| 怀仁县| 武穴市| 隆子县| 瑞丽市| 会东县| 英德市| 吉安市| 通许县| 宁德市| 高碑店市| 根河市| 大洼县| 天水市| 武邑县| 儋州市| 茌平县| 连州市| 微博| 祁连县| 黔江区| 罗城| 甘洛县| 临安市| 阿克苏市| 嘉峪关市| 龙门县| 黄龙县| 昌图县| 涡阳县| 昭平县| 民乐县| 河西区| 富裕县| 吉水县| 阳新县| 六盘水市| 河西区| 朔州市| 宝山区|