json排序 摘自百度
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、数字排序
console.log(obj);
2、字符串排序
console.log(obj);
二、JSON排序例子2
{
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排序 摘自百度的更多相关文章
- 对json排序
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 数组-去重、排序方法、json排序
1.数组去重 /*方法一: 1,'1' 会被认为是相同的; 所有hash对象,如:{x;1},{y:1}会被认为是相同的 //10ms */ Array.prototype.unique=functi ...
- json排序 及替换在字符串中全部替换某字符串
var roadLine = '@ViewBag.RoadLine'; var jsonRoadLine = JSON.parse(roadLine.replace(/"/g, '\&quo ...
- js json 排序
/* json:需要排序的json key:排序项 */ function JsonSort(json, key) { //console.log(json); for (var j = 1, jl ...
- 百度地图api(摘自百度)
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 介绍 JSON(摘自网络)
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Programming Lan ...
- jquery json实现面向对象 百度十二星座
效果: 源码: index.html <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- JSON排序
//排序之前 var arrs=[{"PageID":"1"},{"PageID":"10"},{"PageI ...
- json 排序
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
随机推荐
- CSS中定位机制的想法
对于一个刚刚接触css的新手而言,CSS的定位机制可能是最让人头疼的一件事情了, 接下来我们了解一下CSS的定位机制. position:static | relative | absolute | ...
- mysql远程连接命令(转)
一.MySQL 连接本地数据库,用户名为"root",密码"123"(注意:"-p"和"123" 之间不能有空格) C: ...
- android 内存问题
借鉴:大苞米的博客(http://blog.csdn.net/a396901990) 一.内存溢出(OOM--out of memory) (1)内存溢出引发的问题展现: 1.程序卡顿,响应速度慢(内 ...
- Eclipse创建Maven时提示错误could not resolve archetype
今天用Eclipse创建Maven多模块项目的时候提示错误: could not resolve archetype ******release from any of the configured ...
- 防止多次领取红包进行ID锁
//controller里面使用锁 ActivityRedPacket ap = customerService.getActivityRedPacket(params); if (synchr ...
- C#时间操作
C#时间戳与日期互转 /// <summary> /// 时间戳转为C#格式时间 /// </summary> /// <param name="timeSta ...
- 社区O2O的发展与未来
虽然很多人都自我标榜为社区O2O,其实,在现在这个时间点上,社区O2O可以说是根本不存在的. 社区是什么?对用户来说,社区包括房子,包括邻居,包括宠物,包括保安,包括广场舞,也包括跳广场舞的大妈, ...
- 重启eclipse color theme失效的解决办法
For Eclipse Mars users: In the main menu bar, go to Window > Preferences In the preference tree o ...
- RSA For PHP
最近和一保险公司对接接口,对方要求RSA加密,并给一个*.jks的文件,网上搜索一番均无答案,最后在谷歌上偶然看到一个人说需要转成.pem进行读取,折腾一番直接上代码: /** * 获取RSA加密ke ...
- Qt MVC(模型-视图-代理)
实习刚才是一段时间,公司这边就要求熟悉这个mvc.一般开始都是用tableview,前面的blog我都是使用listview居多,并且相对delegate这个使用的多余model.接下来说下model ...