2017-08-21 1 views
-5

Ich habe eine Reihe von Objekten, die unterJavascript Sortierung von Nullwerten in Betracht gezogen werden

let customerData = [{ 
     id: 1, 
     name: 'cust1', 
     address: null 
    }, 
    { 
     id: 2, 
     name: 'cust2', 
     address: 'test1' 
    }, 
    { 
     id: 3, 
     name: 'cust3', 
     address: 'add2' 
    }, 
    { 
     id: 4, 
     name: 'cust4', 
     address: 'test2' 
    }, 
    { 
     id: 5, 
     name: 'cust5', 
     address: null 
    } 
]; 

In ASC Nullwerte auch ähnlich dem ist/desc die Nullwerte werden immer am last.Can jemand sortiert bitte helfen Sie mir, die Null-Werte auch zu sortieren? Bewegen Sie entweder alle Null-Werte nach oben, wenn Sie asc/desc.

Wenn wir die Nullwerte durch Leerzeichen ersetzen, funktioniert die Sortierung wie erwartet. Aber wir können die Daten nicht in leer ändern, da die Null- und Leerwerte unterschiedlich behandelt werden.

Ich habe viele ähnliche Beiträge gesehen, aber alle von ihnen machen die Nullwerte, um unten wie die Links unten zu drücken.

How to sort an array with null values

Bitte nicht diese Frage, als Duplikat markiert, wie ich keine richtige Lösung gefunden.

Die Lösungen ich habe

versucht
data.sort(function (item1, item2) { 
    ------------------/////---not working------------------------------ 
if (item1[colBinding] === null && sort.direction === 'asc') return 1; 
if (item2[colBinding] === null && sort.direction === 'asc') return 0; 
if (item1[sort.colBinding] === null && sort.direction === 'desc') return 1;    if (item2[sort.colBinding] === null && sort.direction === 'desc') return -1; 
if (sort.direction === 'asc') return item1[sort.colBinding] > item2[sort.colBinding]; 
if (sort.direction === 'desc') return item1[sort.colBinding] < item2[sort.colBinding]; 

------------------------------------------------------- 
----------------------//working to push null at the end 
let currentData = item1[colBinding] === null ? '' : item1[sort.colBinding]; 
let nextData = item2[sort.colBinding] === null ? '' : item2[sort.colBinding]; 
if (currentData === nextData) { return -1 }; 
if (currentData < nextData) { return 0 }; 
if (currentData > nextData) { return 1 }; 
---------------------------------------------------------- 

---------------------/// not working----------------------------------- 
var nullPosition = sort.direction === 'asc' ? 1 : -1; 
if (item1[colBinding] === null) return nullPosition; 
if (item2[colBinding] === null) return -nullPosition; 
if (item1[colBinding] < item2[colBinding]) return -nullPosition; 
if (item1[colBinding] > item2[colBinding]) return nullPosition; 
return 0 
------------------------------------------------------------------------------ 

       }); 
+0

Format Code –

+0

Die Antwort, die Sie nicht unter Push verbunden ... Es hängt von der Sortierreihenfolge ... Und man kann es leicht bearbeiten, wenn nötig. Definitiv ein Duplikat ... – Salketer

+3

Mögliches Duplikat von [Wie man ein Array mit Nullwerten sortiert] (https://stackoverflow.com/questions/33684937/how-to-sort-an-array-with-null-values) – Salketer

Antwort

1

Sie könnten den Wert für null zuerst überprüfen und sortieren sie, nach oben dann sortiert nach dem Wert.

var data = [{ id: 1, name: 'cust1', address: null }, { id: 2, name: 'cust2', address: 'test1' }, { id: 3, name: 'cust3', address: 'add2' }, { id: 4, name: 'cust4', address: 'test2' }, { id: 5, name: 'cust5', address: null }]; 
 

 
data.sort(function (a, b) { 
 
    return (b.address === null) - (a.address === null) || ('' + a.address).localeCompare(b.address); 
 
}); 
 

 
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Für absteigend sortieren, Reverse nur den zweiten Teil

var data = [{ id: 1, name: 'cust1', address: null }, { id: 2, name: 'cust2', address: 'test1' }, { id: 3, name: 'cust3', address: 'add2' }, { id: 4, name: 'cust4', address: 'test2' }, { id: 5, name: 'cust5', address: null }]; 
 

 
data.sort(function (a, b) { 
 
    return (b.address === null) - (a.address === null) || ('' + b.address).localeCompare(a.address); 
 
}); 
 

 
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

+0

Hallo, Vielen Dank für die Antwort. Es hat mir sehr geholfen. Da ich die oben abgebildete Null in absteigender Reihenfolge benötigte, habe ich den absteigenden Code wie folgt umgekehrt: - data.sort (function (a, b)) { return (a.Adresse === null) - (b.Adresse == = null) || ('' + b.Adresse) .localeCompare (a.Adresse); }); – Mothy

+0

Der erste Teil bewegt 'null' Werte nach oben oder unten. Wenn die Änderung für Sie arbeitet, ist es in Ordnung. –

0

Sie eine benutzerdefinierte Sortierfunktion verwenden können, um das Array zu sortieren und einen Parameter verwenden um die Sortierreihenfolge anzugeben.

var data = [{ id: 1, name: 'cust1', address: null }, { id: 2, name: 'cust2', address: 'test1' }, { id: 3, name: 'cust3', address: 'add2' }, { id: 4, name: 'cust4', address: 'test2' }, { id: 5, name: 'cust5', address: null }]; 
 

 
var sortWithOrder = (data, order) => { 
 
    let r = order === 'ASC' ? 1 : -1; 
 
    return data.sort(function (a, b) { 
 
    if(!a.address && !b.address) 
 
    return 0; 
 
    else if(!b.address && a.address) 
 
    return 1; 
 
    else if(!a.address && b.address) 
 
    return -1; 
 
    else 
 
    return r * a.address.localeCompare(b.address); 
 
    }); 
 
} 
 

 
console.log(sortWithOrder(data, 'ASC')); 
 
console.log(sortWithOrder(data, 'DESC'));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Verwandte Themen