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/ ...
随机推荐
- 如何用Unity制作自定义字体——Custom Font
一.效果图 二.步骤 将美术做好的字体分块导入BMFont,使用BMFont工具生成艺术字库: 将上面的数据导入unity资源目录下:*.fnt文件中记录每个文字的状态信息: 导入*.png图片并设置 ...
- 伸缩盒 Flexible Box(旧)
box-orient box-pack box-align box-flex box-flex-group box-ordinal-group box-direction box ...
- Visual Studio 2015 下 编译 libpng
libpng https://github.com/glennrp/libpng zlib https://github.com/madler/zlib/releases https://github ...
- PHP 之 this self parent static 对比
this 绑定的是当前已经实例化的对象 这样长出现的问题: 如果你在一个父类中使用$this调用当前一个类的方法或者属性,如果这个类被继承,且在相应的子类中有调用的属性或者方法是,则父类中的$this ...
- (转)Android消息处理机制(Handler、Looper、MessageQueue与Message)
转自 http://www.cnblogs.com/angeldevil/p/3340644.html Android消息处理机制(Handler.Looper.MessageQueue与Messag ...
- My安卓知识4--Media Player called in state 0
//根据被传递的歌曲名称,选择播放的歌曲 public void playByName(String name){ mp = new MediaPlayer(); t ...
- 一台独立的服务器是可以可以建立多个网站的,一个ip地址,一个端口
# 1,若开启虚拟主机,这个一定要有(IP:端口),找到 #NameVirtualHost *:80 修改成: NameVirtualHost 127.0.0.1:80 # 2,修改<Direc ...
- iOStextFiled判断输入长度
个人在开发当中发现在用textField的代理方法 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(N ...
- 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 ...
- Android(Xamarin)之旅(五)
2016年1月23日,北京迎来了很痛苦的一天,冻死宝宝了,一天都没有出我自己的小黑屋,在这屋子里自娱自乐.不知道你们呢 对于android的四大基本组件(Activity.Service.Broadc ...