我希望有一个函数来通过获取包含属性名称和排序方向的输入字符串来对对象数组进行排序。我需要这样的东西:
var myArray = [{name:"A", age: 30}, {name:"B", age:20}, {name:"C", age:20}];
var strSort = "age asc, name desc";
var sortedArray = customSortFuntion(myArray,strSort);
//sortedArray == [{name:"C", age:20}, {name:"B", age:20},{name:"A", age: 30}]
function customSortFuntion(myArray,strSort)
{
//return sorted by age asc and sorted by name desc etc..??
}
答案 0 :(得分:2)
此解决方案需要Array.prototype.map
(“shim”-able)才能显示:
function customSortFunction(myArray,strSort){
var sorts=strSort.split(",").map(function(v,i){
var o=v.match(/\s(asc|desc)$/i);
if(o){
return {"prop":v.replace(/\s(asc|desc)$/i,"").replace(/^\s+|\s+$/,""),"order":o[1].toLowerCase()};
}else{
return {"prop":v,"order":"asc"};
}
});
myArray.sort(function(a,b){
var av,bv;
for(var i=0;i<sorts.length;i++){
av=a[sorts[i]["prop"]] || 0;
bv=b[sorts[i]["prop"]] || 0;
if(sorts[i]["order"]=="asc"){
if(av>bv){
return 1;
}else if(bv>av){
return -1;
}
}else{
if(av>bv){
return -1;
}else if(bv>av){
return 1;
}
}
}
return 0;
});
return myArray;
}
鉴于此数组:
[
{name:"Z", age: 6},
{name:"Z", age: 19},
{name:"A", age: 30},
{name:"B", age: 20},
{name:"C", age: 20},
{name:"A", age: 31}
]
并排序name asc, age desc
,
给出了这个输出:
[ { name: 'A', age: 31 },
{ name: 'A', age: 30 },
{ name: 'B', age: 20 },
{ name: 'C', age: 20 },
{ name: 'Z', age: 19 },
{ name: 'Z', age: 6 } ]
答案 1 :(得分:1)
这是我的解决方案,它是一个自定义排序函数,它接受规则数组,拆分它并检查属性值。
使用括号表示法动态命名属性值:foo['bar']
代码(试了一下):
function customSortFuntion(myArray, strSort) {
var sortable = myArray;
var sortRules = strSort.split(',')
return sortable.sort(function (a, b) {
for (var s = 0; s < sortRules.length; s++) {
if (sortRules[s].split(' ')[1] == "asc") {
if (a[sortRules[s].split(' ')[0]] < b[sortRules[s].split(' ')[0]]) {
return 1
}
} else {
if (a[sortRules[s].split(' ')[0]] > b[sortRules[s].split(' ')[0]]) {
return 1
}
}
}
})
}
演示:http://jsfiddle.net/IrvinDominin/8Z9xc/
您可以使用正则表达式在存在更多空格时进行拆分。
代码:
function customSortFuntion(myArray, strSort) {
var sortable = myArray;
var sortRules = strSort.split(',')
return sortable.sort(function (a, b) {
for (var s = 0; s < sortRules.length; s++) {
var sortRule=sortRules[s].split(/ +/)
if (sortRule[1] == "asc") {
if (a[sortRule[0]] < b[sortRule[0]]) {
return 1
}
} else {
if (a[sortRule[0]] > b[sortRule[0]]) {
return 1
}
}
}
})
}
答案 2 :(得分:1)
如果你改变它会更容易:
"age asc, name desc"
进入其中一个:
[["age", "asc"], ["name", "desc"]]
[{ key: "age", dir: "asc" }, { key: "name", dir: "desc"}]
这是一个实用功能,它可以做到:
function transformSortString(sortString) {
var sortParams = [],
temp = sortString.match(/[^ ,]+/g),
i;
for (i = 0; i < temp.length; i += 2) {
sortParams.push({
key: temp[i],
dir: temp[i + 1]
});
}
return sortParams;
}
这是与Array.sort一起使用的多重排序功能。非常简单;将 a 与 b 列逐列比较,如果值不同,则返回-1或+1:
function customSortFuntion(array, sortParams) {
array.sort(function (a, b) {
var i;
for (i = 0; i < sortParams.length; i++) {
if (a[sortParams[i].key] !== b[sortParams[i].key]) {
if (a[sortParams[i].key] < b[sortParams[i].key]) {
return sortParams[i].dir === "asc" ? -1 : 1;
}
if (a[sortParams[i].key] > b[sortParams[i].key]) {
return sortParams[i].dir === "asc" ? 1 : -1;
}
}
}
return 0;
});
}
以下是您使用它的方式:
var myArray = [
{ name: "A", age: 10 },
{ name: "B", age: 10 },
{ name: "C", age: 10 },
];
customSortFuntion(myArray, transformSortString("age asc, name desc"));
这是一个演示:
答案 3 :(得分:0)
这是逻辑:
function myFunction()
{
var arr = [[13, 'AAA'],[48, 'BBB'],[28, 'CCC'],[38, 'DDD']];
// First column * desc
arr = arr.sort(function(a,b) {
return b[0] > a[0];
});
// second column *ascc
arr = arr.sort(function(a,b) {
return a[1] > b[1];
});
}
答案 4 :(得分:0)
var myarray = [{name:'a',age : 30},{name:'b',age:20}];
var mysort = function(array,string){
for(var i=0; i<array.length; i++){
for(var key in array[i]){
if(array[i][key] == string){
console.log(array[i])
}
}
}
}
var b = mysort(myarray,'a');
//result {name = 'a', age = 30}
使用此代码,可能会有帮助。
答案 5 :(得分:0)
var myArray = [{name:"A", age: 30}, {name:"B", age:20}];
var strSort = "name asc, age asc";
var sortedArray = customSortFuntion(myArray,strSort);
function customSortFuntion(myArray,strSort)
{
var myarr = strSort.split(",");
// create a loop with 'myarr length'
var mysortarr=myarr[1].split(" ");
// mymysortarr 1 gives name and 2 gives asc or desc
if(mysortarr[2]=='desc')var entry= arr.sort(function(a,b) {
return b[0] > a[0];
});
if(mysortarr[2]=='asc')var entry = arr.sort(function(a,b) {
return a[1] > b[1];
});
alert(JSON.stringify(entry));
// end loop
}