node,javascript数组排序,去重复,并集,交集,差集

/**
 * Created by lht on 14-7-14.
 */
var arr1=[1,2,2,3,4,4,3,45,6,7,5];
var arr2=[3,4,5,6,56];
var arr3=[]


//var arr1=['Jani','Hege','Stale','Kai','Jim','Borge','Tove','Jani','Hege'];
//var arr2=['Tome','Hege','Stale','Michale','Amy'];
//var arr3=[]

//输入的是否为数组或空数组
var nullArray=function(arr){
    if(Array.isArray(arr)){
        if(arr.length===0) return false
        return true
    }
    return false;
}
console.log(nullArray(arr1));  //true
console.log(nullArray(arr3));  //false

//去掉重复项
var arrayUnique=function(arr){
    var result=[];
    var l=arr.length;
    if(nullArray(arr)){
       for(var i=0;i pivot ? left.push(arr[i]): right.push(arr[i]);
        }
    }
    return sortObj(left,key,dir).concat(pivotObj, sortObj(right,key,dir));
}
var employees=[]
employees[0]={name:"George", age:32, retiredate:"March 12, 2014"}
employees[1]={name:"Edward", age:17, retiredate:"June 2, 2023"}
employees[2]={name:"Christine", age:58, retiredate:"December 20, 2036"}
employees[3]={name:"Sarah", age:62, retiredate:"April 30, 2020"}
console.log(sortObj(employees,'age'));
/*
[ { name: 'Edward', age: 17, retiredate: 'June 2, 2023' },
{ name: 'George', age: 32, retiredate: 'March 12, 2014' },
{ name: 'Christine', age: 58, retiredate: 'December 20, 2036' },
{ name: 'Sarah', age: 62, retiredate: 'April 30, 2020' } ]*/

console.log(sortObj(employees,'age','desc'));
/*
[ { name: 'Sarah', age: 62, retiredate: 'April 30, 2020' },
{ name: 'Christine', age: 58, retiredate: 'December 20, 2036' },
{ name: 'George', age: 32, retiredate: 'March 12, 2014' },
{ name: 'Edward', age: 17, retiredate: 'June 2, 2023' } ]*/


 
 

你可能感兴趣的:(数组,nodejs)