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、数字排序

复制代码 代码如下:
obj.sort(sortBy('b', false, parseInt));
console.log(obj);

2、字符串排序

复制代码 代码如下:
obj.sort(sortBy('b', false, String));
console.log(obj);

二、JSON排序例子2

复制代码 代码如下:
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 对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,'age');
console.log(json);

三、JSON排序例子3

复制代码 代码如下:

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');

json排序 摘自百度的更多相关文章

  1. 对json排序

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. 数组-去重、排序方法、json排序

    1.数组去重 /*方法一: 1,'1' 会被认为是相同的; 所有hash对象,如:{x;1},{y:1}会被认为是相同的 //10ms */ Array.prototype.unique=functi ...

  3. json排序 及替换在字符串中全部替换某字符串

    var roadLine = '@ViewBag.RoadLine'; var jsonRoadLine = JSON.parse(roadLine.replace(/"/g, '\&quo ...

  4. js json 排序

    /* json:需要排序的json key:排序项 */ function JsonSort(json, key) { //console.log(json); for (var j = 1, jl ...

  5. 百度地图api(摘自百度)

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  6. 介绍 JSON(摘自网络)

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Programming Lan ...

  7. jquery json实现面向对象 百度十二星座

    效果: 源码: index.html <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  8. JSON排序

    //排序之前 var arrs=[{"PageID":"1"},{"PageID":"10"},{"PageI ...

  9. json 排序

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

随机推荐

  1. 如何用Unity制作自定义字体——Custom Font

    一.效果图 二.步骤 将美术做好的字体分块导入BMFont,使用BMFont工具生成艺术字库: 将上面的数据导入unity资源目录下:*.fnt文件中记录每个文字的状态信息: 导入*.png图片并设置 ...

  2. 伸缩盒 Flexible Box(旧)

    box-orient  box-pack    box-align  box-flex   box-flex-group  box-ordinal-group   box-direction  box ...

  3. Visual Studio 2015 下 编译 libpng

    libpng https://github.com/glennrp/libpng zlib https://github.com/madler/zlib/releases https://github ...

  4. PHP 之 this self parent static 对比

    this 绑定的是当前已经实例化的对象 这样长出现的问题: 如果你在一个父类中使用$this调用当前一个类的方法或者属性,如果这个类被继承,且在相应的子类中有调用的属性或者方法是,则父类中的$this ...

  5. (转)Android消息处理机制(Handler、Looper、MessageQueue与Message)

    转自 http://www.cnblogs.com/angeldevil/p/3340644.html Android消息处理机制(Handler.Looper.MessageQueue与Messag ...

  6. My安卓知识4--Media Player called in state 0

    //根据被传递的歌曲名称,选择播放的歌曲    public void playByName(String name){        mp = new MediaPlayer();        t ...

  7. 一台独立的服务器是可以可以建立多个网站的,一个ip地址,一个端口

    # 1,若开启虚拟主机,这个一定要有(IP:端口),找到 #NameVirtualHost *:80 修改成: NameVirtualHost 127.0.0.1:80 # 2,修改<Direc ...

  8. iOStextFiled判断输入长度

    个人在开发当中发现在用textField的代理方法 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(N ...

  9. maven下读取资源文件的问题(转)

    原文链接:http://shenchao.me/2016/04/20/maven%E4%B8%8B%E8%AF%BB%E5%8F%96%E8%B5%84%E6%BA%90%E6%96%87%E4%BB ...

  10. Android(Xamarin)之旅(五)

    2016年1月23日,北京迎来了很痛苦的一天,冻死宝宝了,一天都没有出我自己的小黑屋,在这屋子里自娱自乐.不知道你们呢 对于android的四大基本组件(Activity.Service.Broadc ...