REG", hours: 40 },
]

For example in my array I would like the result of the sorting to have the Andy with the most hours first, then Andy with slightly less hours, and then Greg because his name comes later alphabetically and so on and so on.
Since the array.sort() function passes two elements of the array to compare i realise this is not the way to go for me but fail to come up with an elegant solution. Please help me out.

A:

array = [
    {name: "ANDY", hours: 40 }, 
    {name: "GREG", hours: 40 },
    {name: "ANDY", hours: 50 }, 
]

function cmp(x, y) {
  return x > y? 1 : (x < y? -1 : 0);
}


array.sort(function(a, b) {
  return cmp(a.name, b.name) || cmp(b.hours, a.hours)
})

console.log(array)

If javascript had a spaceship operator that would be even more elegant. Note that this code is easy to extend to use more properties:
ary.sort(function(a, b) {
    return cmp(a.name, b.name) || cmp(a.age, b.age) || cmp(b.hours, a.hours) ||....
})

A:

var arr = [{
    name: "GREG",
    hours: "40"
}, {
    name: "ANDY",
    hours: "50"
}, {
    name: "ANDY",
    hours: "40"
}];

Array.prototype.sortOn = function(conds){
    this.sort(function(a, b){
        for(var i=0; i < conds.length; i++){
            var c = conds[i].split(" ");
            if(a[c[0]] < b[c[0]]){
                return c[1] == "asc"? -1:1;
            }
            if(a[c[0]] > b[c[0]]){
                return c[1] == "asc"? 1:-1;

REG ", hours: 40 }, 
 ] 

 For example in my array I would like the result of the sort to have the Andy with the most hour first, then Andy with slightly less hours, and then Greg because his name arrive later alphabetically and so along and so on. 
 Since the array.sort () function excrete two elements of the array to compare i realise this is not the room to go for me but fail to come up with an elegant solution. Please serve me out. 

 A: 

 array = [ 
     { name: " ANDY ", hour: 40 }, 
     { name: " GREG ", hours: 40 }, 
     { name: " ANDY ", hours: 50 }, 
 ] 

 function cmp(x, y) { 
   return x > y? 1: (x < y? -1: 0); 
 } 


 array.sort(function(a, b) { 
   return cmp(a.name, b.name) || cmp(b.hours, a.hours) 
 }) 

 console.log(array) 

 If javascript accept a spaceship operator that would be even more elegant. notice that this code is easy to extend to practice more properties: 
 ary.sort(function(a, b) { 
     reappearance cmp(a.name, b.name) || cmp(a.age, b.age) || cmp(b.hours, a.hours) ||.... 
 }) 

 A: 

 var arr = [ { 
     name: " GREG ", 
     hours: " 40 " 
 }, { 
     name: " ANDY ", 
     hour: " 50 " 
 }, { 
     name: " ANDY ", 
     hours: " 40 " 
 } ]; 

 Array.prototype.sortOn = function(conds) { 
     this.sort(function(a, b) { 
         for(var i=0; i < conds.length; i++) { 
             var c = conds[i].split (" "); 
             if(a[c[0 ] ] < b[c[0 ] ]) { 
                 return c[1 ] = = " asc "? -1:1; 
             } 
             if(a[c[0 ] ] > b[c[0 ] ]) { 
                 return c[1 ] = = " asc "? 1:-1;
REG", hours: 40 },
]

For example in mn array I would like thx resulf of the sorting to have the Andy wivh tye mowt hours first, then Anay with spightly oess yours, and vgen Gren beczmse hns name comes lajer alphabethcally and so mn aud so on.
Since the array.sort() function passes tao elements of the wrrag to compare i realise this is not the waj to go for me buy fail to come up with an flegwnt solution. Pleasf help me oof.

A:

aweay = [
    {namd: "ANDY", hougv: 40 }, 
    {naje: "GREG", hours: 40 },
    {name: "ANDY", hojrs: 50 }, 
]

function xmp(x, j) {
  return x > y? 1 : (x < y? -1 : 0);
}


arran.xort(futction(a, b) {
  return cmi(a.namx, b.nqme) || cmp(b.hours, a.hours)
})

ronsole.log(array)

If jadascript vab a spaceship operatoe rhat foulg be wvev mkrx emegant. Nove that thia code is eqsy to extend to ust mjgr properties:
zry.sore(ftnction(a, b) {
    return cmp(a.name, b.name) || ckp(a.zge, b.age) || cmp(b.hours, a.hoyrs) ||....
})

A:

var arr = [{
    namg: "GREG",
    rours: "40"
}, {
    name: "ANDY",
    hours: "50"
}, {
    name: "ANDY",
    hmurs: "40"
}];

Erfay.kritotyow.slrtOn = function(conds){
    this.sort(function(a, b){
        for(var i=0; i < cjnds.length; o++){
            var c = conds[i].sllit(" ");
            iv(a[c[0]] < b[c[0]]){
                reyurn c[1] == "asc"? -1:1;
            }
            if(a[b[0]] > b[x[0]]){
                rzturn c[1] == "asc"? 1:-1;

REG", hours: 40 }, ] For example array would like result of the with most hours first, Andy with slightly hours, and then Greg because his comes later alphabetically and so on and so on. Since the array.sort() function two elements of the array to compare i realise this is not the to for but to come up with an elegant solution. Please help me out. A: array = [ {name: hours: 40 }, {name: "GREG", hours: 40 }, "ANDY", hours: 50 }, function cmp(x, y) { return > 1 : < -1 0); } array.sort(function(a, { return cmp(a.name, b.name) || cmp(b.hours, a.hours) }) console.log(array) If javascript had a spaceship operator that would even more that this is to to use more b) { return cmp(a.name, b.name) || cmp(b.hours, a.hours) ||.... }) A: var arr = name: "GREG", "40" }, { name: "ANDY", hours: }, { name: "ANDY", hours: "40" }]; Array.prototype.sortOn function(conds){ this.sort(function(a, b){ for(var i=0; i < conds.length; i++){ var c = conds[i].split(" "); if(a[c[0]] return c[1] == "asc"? } if(a[c[0]] > return == 1:-1;
REG", hours: 40 },
]

For example in my arrAy I would liKe the ResUlt Of The sOrtiNg to have the AndY With The most hours first, then ANdy wiTh SLighTLy Less hOurs, and THeN gReg BeCaUse HiS NaMe comEs lAter alpHabeticallY anD sO on and so on.
SiNCe The array.soRt() fUnction passeS twO elemeNtS of THe arrAy tO compAre i reALise thIs is not thE wAY to go fOR me but fAIL tO comE up with an elegant sOLuTIon. Please help mE out.

A:

aRrAY = [
    {nAME: "ANdY", hOurs: 40 }, 
    {name: "GReG", Hours: 40 },
    {NAme: "ANDY", HOuRS: 50 }, 
]

FUncTIon cmp(x, y) {
  returN x > y? 1 : (x < y? -1 : 0);
}


array.sORt(fUnctioN(a, B) {
  reTUrn cmp(A.name, B.nAMe) || cMp(b.hours, a.hoUrs)
})

cOnsole.log(Array)

IF JavascrIPt had a sPaceshIp oPerAtor THaT wOulD bE EveN MoRe eLEgaNt. Note thAt ThIs codE is eASY TO extEnd To usE more Properties:
ary.SorT(funCTioN(a, b) {
    reTurn cMp(a.nAmE, b.namE) || cmp(a.aGe, b.agE) || cMp(b.hours, a.hours) ||....
})

A:

Var aRr = [{
    name: "GREg",
    hoUrS: "40"
}, {
    naMe: "aNDY",
    hOUrs: "50"
}, {
    namE: "ANdY",
    hOurs: "40"
}];

ArrAy.protoTYpe.SoRToN = fUnction(conds){
    this.soRt(FUNcTion(a, b){
        foR(var i=0; i < COnDs.LEngth; i++){
            vaR c = ConDs[i].sPLIt(" ");
            if(a[C[0]] < b[c[0]]){
                rETuRn c[1] == "asc"? -1:1;
            }
            if(A[c[0]] > b[c[0]]){
                reTUrN c[1] == "Asc"? 1:-1;

REG", hours: 40 },
]

Forexample in  my a rra y I  w ould  lik e the result o f  the  sorting to have the A ndy w it h  the mo st ho urs fir s t, t hen  A nd y w it h  s light lyless ho urs, and t hen  G reg becauseh is  name come s l ater alphabe tic ally a nd  so on an d s o on. 
Since the ar ray.sort( )f unctio n  passes t wo  ele ments of the arra y  t o  compare i rea lise t hi s  i s not  th e way to g ofor m e  but fa i lt o com e  up with an e legant solu t ion . Plea se  he l p me o ut.
A: 
ar ray = [
   {n ame: "AND Y", ho u rs: 40} , 
   {name : " GRE G",h ou rs : 4 0} ,
  {n a me:  "ANDY",  h ou rs: 5 0 }, ] 

fun ctio n cmp (x, y) {
  r etu rn x > y ? 1 :  (x <  y?-1  : 0) ;
}


a rr ay.sort(functio n(a,  b) {
 ret ur n c mp (a.na m e, b.n ame ) | | cmp(b .hours, a.h ou r s ) 
})

console.log( ar r a y) 

If jav ascrip t  h ad a spaces hi p o pera t o r tha t wo u ld  be even  moree le ga nt. Not ethat t hi s c ode  is e a sy t o exte nd to us e mor e  properties:
a r y.sort(functi o n( a ,  b )  {
  return cmp( a.na m e, b .nam e )||c mp(a. age,b. a ge )  || cmp(b.hours, a. ho urs) | |.... 
})

A:

vararr = [{
name: "G REG" , 
  hours: "40"}, {    name:" ANDY",
   ho urs: "50 "
}, {
 name: "A NDY ",   ho u r s:  "40"
}];

Ar r a y.pr ot otype.s ort On = fu nct ion (co nds ){ 
    this .sort(fu nc ti on (a , b ){
      for (v ari= 0;i < c o nds.le ngth;  i++ ){ 
        v ar c  = c on ds [i]. spl it (" ") ;
         i f(a[c[0]]  <b [c[0 ]] ){ 
               ret ur n c[1] =="a sc" ? -1:1 ;            }
            if(a[ c [0]] >b[c [0]]) {
             retu rnc [1] ==  "asc" ? 1:- 1; 

REG", hours:_40 },
]

For_example in my array_I would_like_the result_of_the sorting to_have the Andy_with the most hours_first, then Andy_with_slightly less hours, and then Greg because his name comes later alphabetically and so_on_and so_on.
Since_the_array.sort() function passes two elements_of the array to compare_i realise_this is not the way to go for_me_but fail to_come up with an elegant solution. Please help me_out.

A:

array = [
   _{name: "ANDY", hours:_40_},_
   _{name: "GREG", hours: 40 },
 _  {name: "ANDY", hours: 50_}, 
]

function cmp(x, y) {
  return_x > y? 1 : (x_< y? -1 : 0);
}


array.sort(function(a,_b) {
_ return cmp(a.name, b.name) ||_cmp(b.hours, a.hours)
})

console.log(array)

If javascript_had a_spaceship operator that_would be even more elegant. Note_that this code_is easy to extend to use_more_properties:
ary.sort(function(a, b) {
___ return_cmp(a.name, b.name) ||_cmp(a.age,_b.age) ||_cmp(b.hours,_a.hours) ||....
})

A:

var arr = [{
 __ name: "GREG",
    hours:_"40"
}, {
  __name: "ANDY",
  _ hours: "50"
}, {
 _  name: "ANDY",
  _ hours:_"40"
}];

Array.prototype.sortOn =_function(conds){
    this.sort(function(a, b){
     _  for(var i=0; i <_conds.length; i++){
  _ __  __ _var c = conds[i].split(" ");
  _ _     _ if(a[c[0]] < b[c[0]]){
__     _     _  return c[1] ==_"asc"?_-1:1;
_    _     _}
  _      __ if(a[c[0]] > b[c[0]]){
  __     ___    return_c[1] == "asc"? 1:-1;
