var arr = [
{"symbolone": "rb1905", "symboltwo": "rb1910", "name": "螺纹"},
{"symbolone": "a1905", "symboltwo": "a1909", "name": "黄大豆"},
{"symbolone": "zn1905", "symboltwo": "zn1908", "name": "锌"}
];
function sortArr(arr, sortStr) {
var bySort = function() {
return function(o, p) {
var a = o[sortStr],
b = p[sortStr];
if (isNaN(a)) {
return a.localeCompare(b);
} else {
if (a === b) {
return 0;
} else {
return a > b ? 1 : -1;
}
}
}
};
for (var i = 0; i < arr.length; i++) {
arr.sort(bySort(arr[i][sortStr]));
}
}
sortArr(arr, 'symbolone'); // 按 symbolone 排序
//sortArr(arr, 'symboltwo'); // 按 symboltwo 排序
// sortArr(arr, 'name'); // 按 中文名 cnName 排序
console.log(arr);
// 简单写法
var person = [
{name: 'wangw', age: 24},
{name: 'guofq', age: 26},
{name: 'gfq', age: 16},
{name: 'zhangs', age: 18},
{name: 'lis', age: 22},
{name: 'allan', age: 4}
]
person.sort(function(a, b){
return a.name.localeCompare(b.name);
})
console.log(person);
// 得到前天,今天,后天的日期:eg:今天20190325 , 那么得到前天 20190323 , 后天 20190327
function num(s) {
return s < 10 ? '0' + s : s;
}
function GetDateStr(AddDayCount) {
var date = new Date();
date.setDate(date.getDate() + AddDayCount);//获取AddDayCount天后的日期
var y = date.getFullYear();
var m = date.getMonth() + 1;
var d = date.getDate();
return y + num(m) + num(d);
}
console.log("7天前:"+GetDateStr(-7));
console.log("前天:"+GetDateStr(-2));
console.log("今天:"+GetDateStr(0));
console.log("后天:"+GetDateStr(+2));
网友评论