2013-06-30 21 views
17

Zobacz edycję na końcu dla rzeczywistego problemu.Tablica sortowania JavaScript obiektów według właściwości boolowskiej

Ok, mam następujący scenariusz:

a = [false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false] 

Następnie, jeśli mogę to zrobić:

a.sort(function(a,b){return !a && b}); 

Daje mi to:

[false, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, false, true, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false] 

To jakoś robi coś w rodzaju ... ale nie do końca ... :(

Jak sortować tę tablicę?

EDIT:

Jeśli zastanawiasz się, dlaczego nie użyć po prostu a.sort() jest, bo moja rzeczywista tablica jest obiektów, a nie zwykły szyk jak ten napisałem. Prawdziwy jeden posiada elementy, które wyglądają jak [{xx: true}, {xx: false}, ...]

+0

Jeśli robię a.map (function (x) {return x 1: 0}). Sort (function (a, b) {return a> b}); też nie działa ... Myślę, że mógłbym zrobić coś elementarnego nie tak – PCoelho

+0

dlaczego trzeba pisać niestandardową funkcję? a.sort() powinien działać –

Odpowiedz

59

a = [false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]; 
 
    
 
    
 
    a.sort(function(x, y) { 
 
     // true values first 
 
     return (x === y)? 0 : x? -1 : 1; 
 
     // false values first 
 
     // return (x === y)? 0 : x? 1 : -1; 
 
    }); 
 
    
 
    console.log(a);

Musisz zwraca 0 gdy A i B mają ta sama wartość, -1, jeśli a jest prawdziwe, a 1 w przeciwnym razie.

+1

Tak, masz to :) dziękuję! (Przyjmuję twoją odpowiedź, gdy tylko stackoverflow pozwoli mi: P) – PCoelho

+13

Jeszcze krócej: a.sort (funkcja (x, y) {return y - x}); –

+2

a co z a.sort()? ed: wypróbowany, działa! – dandavis

15

prostszy sposób:

a = [{xx:true},{xx:false},{xx:true},{xx:false},{xx:true},{xx:false},{xx:true},{xx:false},{xx:true},{xx:false},{xx:true},{xx:false},{xx:true},{xx:false},{xx:true},{xx:false},{xx:true},{xx:false}]; 

a.sort(function(a,b){return a.xx-b.xx}); 

console.log(a); 

można nazwać a.reverse() po sort(), jeśli chcesz to klasyfikowane w drugą stronę ..

EDIT: edytowane w celu odzwierciedlenia zaktualizowaną pytanie sortowania tablicy obiektów zamiast tablicy wartości logicznych.

+0

Tak, co jest nie tak z a.sort(), OP? –

+1

@PranavNegandhi: Co jest nie tak, to że odpowiedziałem na pytanie przed zmianą ponownie zdefiniowałem ... – dandavis

+0

zaktualizował odpowiedź tak, aby ludzie mogli wycinać i wklejać test ... – dandavis

0

PFB rozwiązanie pracował dla mnie w maszynopisie kątowej 2, jak również,

let a = [{aa:"1",xx:true},{aa:"10",xx:false},{aa:"2",xx:true},{aa:"11",xx:false},{aa:"3",xx:true},{aa:"12",xx:false},{aa:"4",xx:true},{aa:"13",xx:false},{aa:"5",xx:true},{aa:"14",xx:false},{aa:"6",xx:true},{aa:"15",xx:false},{aa:"7",xx:true},{aa:"16",xx:false},{aa:"8",xx:true},{aa:"17",xx:false},{aa:"9",xx:true},{aa:"18",xx:false}]; 

    //a.sort(function(a,b){return a.xx-b.xx}); 
    a.sort(function (x, y) { 
     // true values first 
     return (x.xx === y.xx) ? 0 : x ? -1 : 1; 
     // false values first 
     // return (x === y)? 0 : x? 1 : -1; 
    }); 
    return JSON.stringify(a); 
Powiązane problemy