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. 简单的html和css

    整体图太大了,看不太清楚,下面是分开的图 第一张: 第二张:

  2. 自用debug单元

    将之前的内存查看单元小幅修改,加上文件操作和计时,组成了一个自用debug单元,使用方法如示例. 此单元便捷之处在于直接将#define DEBUG注释掉而无需改动源码,即可取消debug模式. #d ...

  3. 我的 C++ style

    int g_tennis; // not use as possible int make_world() { ; return size; } ; enum Color { RED, GREEN } ...

  4. centos从日志文件查找关键字的日志并生成文件

    grep "unset user wechat user_id:" app* | tee wechat_log

  5. Itext Demo

    Tables and fonts /** * Example written by Bruno Lowagie in answer to the following question: * http: ...

  6. js汉语转拼音(全拼、首字母、拼音首字母)

    新建js文件first_alphabet.js // JavaScript Document // 汉字拼音首字母列表 本列表包含了20902个汉字,用于配合 ToChineseSpell //函数使 ...

  7. Asp.Net MVC4入门指南(7):给电影表和模型添加新字段

    在本节中,您将使用Entity Framework Code First来实现模型类上的操作.从而使得这些操作和变更,可以应用到数据库中. 默认情况下,就像您在之前的教程中所作的那样,使用 Entit ...

  8. 安装软件 报错1304 C:\Program Files\Common Files\VMware\VMware VMRC Plug-in\Firefox\dbghelp.dll验证您对该目录具有访问权限

    现在情况是C:\Program Files\Common Files\VMware\VMware VMRC Plug-in\Firefox这个文件夹里面没有dbghelp.dll文件,就需要在百度上面 ...

  9. service XXX does not support chkconfig

    有时候为了方便管理,我们常常喜欢在Linux中将之安装为服务,然后就可以使用服务来管理. 但是当我们运行安装服务的命令时候,假设服务名为myservice #chkconfig --add myser ...

  10. MySQL 性能优化的最佳20多条经验分享

    当我们去设计数据库表结构,对操作数据库时(尤其是查表时的SQL语句),我们都需要注意数据操作的性能.这里,我们不会讲过多的SQL语句的优化,而只是针对MySQL这一Web应用最多的数据库.希望下面的这 ...