fork download
  1. const users =[
  2. {id:3, birth:1999, name:'lee'},
  3. {id:1, birth:2001, name:'doe'}
  4. ]
  5.  
  6. function customSort(rows, ...conds){
  7. rows.sort((a,b)=>{
  8. for(let i=0;i<conds.length;i++){
  9. const cond = conds[i].split(' ');
  10. const col = cond[0];
  11. if (a[col]===b[col]) continue;
  12. else {
  13. const r =cond[1]==='desc'?-1:1;
  14. return (a[col]-b[col])*r;
  15. }
  16. }
  17. })
  18. return 0;
  19. }
  20.  
  21. customSort(users, 'birth desc', 'id', 'name asc');
  22.  
  23.  
Success #stdin #stdout 0.03s 16844KB
stdin
Standard input is empty
stdout
[object Object],[object Object]