2017-07-03 67 views
1

我有一個包含狀態和名稱屬性的對象數組。 我想根據使用下面的代碼的name財產上使用它們:錯誤的基於名稱屬性的對象數組

array = array.sort(function(a,b){ 
    return a.name - b.name 
}); 

但是它不返回正確排序的數組。 這裏是一個片段,使您可以在行動中看到它:

var array = [{"name":"whoopsie","status":"active"},{"name":"acpid","status":"active"},{"name":"apache2","status":"active"},{"name":"apparmor","status":"active"},{"name":"apport","status":"active"},{"name":"avahi-daemon","status":"active"},{"name":"bluetooth","status":"active"},{"name":"cron","status":"active"},{"name":"cups","status":"active"},{"name":"cups-browsed","status":"active"},{"name":"dbus","status":"active"},{"name":"grub-common","status":"active"},{"name":"irqbalance","status":"active"},{"name":"kmod","status":"active"},{"name":"lightdm","status":"active"},{"name":"lvm2-lvmetad","status":"active"},{"name":"lvm2-lvmpolld","status":"active"},{"name":"apache-htcacheclean","status":"active"},{"name":"network-manager","status":"active"},{"name":"networking","status":"active"},{"name":"postfix","status":"active"},{"name":"procps","status":"active"},{"name":"resolvconf","status":"active"},{"name":"rsyslog","status":"active"},{"name":"smartmontools","status":"active"},{"name":"smbd","status":"active"},{"name":"speech-dispatcher","status":"active"},{"name":"ssh","status":"active"},{"name":"thermald","status":"active"},{"name":"tlp","status":"active"},{"name":"udev","status":"active"},{"name":"ufw","status":"active"},{"name":"lvm2-lvmpolld","status":"inactive"},{"name":"mysql","status":"active"},{"name":"alsa-utils","status":"inactive"},{"name":"x11-common","status":"inactive"},{"name":"apache-htcacheclean","status":"inactive"},{"name":"avahi-daemon","status":"inactive"},{"name":"console-setup.sh","status":"inactive"},{"name":"cryptdisks","status":"inactive"},{"name":"cryptdisks-early","status":"inactive"},{"name":"cups-browsed","status":"inactive"},{"name":"grub-common","status":"inactive"},{"name":"hwclock.sh","status":"inactive"},{"name":"kerneloops","status":"inactive"},{"name":"keyboard-setup.sh","status":"inactive"},{"name":"laptop-mode","status":"inactive"},{"name":"lvm2","status":"inactive"},{"name":"lvm2-lvmetad","status":"inactive"},{"name":"anacron","status":"inactive"},{"name":"network-manager","status":"inactive"},{"name":"nmbd","status":"inactive"},{"name":"plymouth","status":"inactive"},{"name":"plymouth-log","status":"inactive"},{"name":"pppd-dns","status":"inactive"},{"name":"prometheus","status":"inactive"},{"name":"rsync","status":"inactive"},{"name":"samba","status":"inactive"},{"name":"samba-ad-dc","status":"inactive"},{"name":"saned","status":"inactive"},{"name":"speech-dispatcher","status":"inactive"},{"name":"unattended-upgrades","status":"inactive"},{"name":"uuidd","status":"inactive"},{"name":"unattended-upgrades","status":"active"}]; 
 
array = array.sort(function (a,b) { 
 
    return a.name - b.name 
 
}) 
 
console.log(array.map(function(i){ 
 
    return i.name 
 
}))
這裏是輸出:

我運行中的NodeJS這個代碼

[ 'whoopsie', 
    'unattended-upgrades', 
    'apache2', 
    'apparmor', 
    'apport', 
    'avahi-daemon', 
    'bluetooth', 
    'cron', 
    'cups', 
    'cups-browsed', 
    'dbus', 
    'grub-common', 
    'irqbalance', 
    'kmod', 
    'lightdm', 
    'lvm2-lvmetad', 
    'lvm2-lvmpolld', 
    'apache-htcacheclean', 
    'network-manager', 
    'networking', 
    'postfix', 
    'procps', 
    'resolvconf', 
    'rsyslog', 
    'smartmontools', 
    'smbd', 
    'speech-dispatcher', 
    'ssh', 
    'thermald', 
    'tlp', 
    'udev', 
    'ufw', 
    'acpid', 
    'mysql', 
    'alsa-utils', 
    'x11-common', 
    'apache-htcacheclean', 
    'avahi-daemon', 
    'console-setup.sh', 
    'cryptdisks', 
    'cryptdisks-early', 
    'cups-browsed', 
    'grub-common', 
    'hwclock.sh', 
    'kerneloops', 
    'keyboard-setup.sh', 
    'laptop-mode', 
    'lvm2', 
    'lvm2-lvmetad', 
    'anacron', 
    'network-manager', 
    'nmbd', 
    'plymouth', 
    'plymouth-log', 
    'pppd-dns', 
    'prometheus', 
    'rsync', 
    'samba', 
    'samba-ad-dc', 
    'saned', 
    'speech-dispatcher', 
    'unattended-upgrades', 
    'uuidd', 
    'lvm2-lvmpolld' ] 

回答

2

你被排序表達式a.name - b.name對於您的數據總是NaN

如果你想按字母順序排序您可能希望看到這個帖子:
Javascript - sort objects in an array alphabetically on one property of the array

var array = [{"name":"whoopsie","status":"active"},{"name":"acpid","status":"active"},{"name":"apache2","status":"active"},{"name":"apparmor","status":"active"},{"name":"apport","status":"active"},{"name":"avahi-daemon","status":"active"},{"name":"bluetooth","status":"active"},{"name":"cron","status":"active"},{"name":"cups","status":"active"},{"name":"cups-browsed","status":"active"},{"name":"dbus","status":"active"},{"name":"grub-common","status":"active"},{"name":"irqbalance","status":"active"},{"name":"kmod","status":"active"},{"name":"lightdm","status":"active"},{"name":"lvm2-lvmetad","status":"active"},{"name":"lvm2-lvmpolld","status":"active"},{"name":"apache-htcacheclean","status":"active"},{"name":"network-manager","status":"active"},{"name":"networking","status":"active"},{"name":"postfix","status":"active"},{"name":"procps","status":"active"},{"name":"resolvconf","status":"active"},{"name":"rsyslog","status":"active"},{"name":"smartmontools","status":"active"},{"name":"smbd","status":"active"},{"name":"speech-dispatcher","status":"active"},{"name":"ssh","status":"active"},{"name":"thermald","status":"active"},{"name":"tlp","status":"active"},{"name":"udev","status":"active"},{"name":"ufw","status":"active"},{"name":"lvm2-lvmpolld","status":"inactive"},{"name":"mysql","status":"active"},{"name":"alsa-utils","status":"inactive"},{"name":"x11-common","status":"inactive"},{"name":"apache-htcacheclean","status":"inactive"},{"name":"avahi-daemon","status":"inactive"},{"name":"console-setup.sh","status":"inactive"},{"name":"cryptdisks","status":"inactive"},{"name":"cryptdisks-early","status":"inactive"},{"name":"cups-browsed","status":"inactive"},{"name":"grub-common","status":"inactive"},{"name":"hwclock.sh","status":"inactive"},{"name":"kerneloops","status":"inactive"},{"name":"keyboard-setup.sh","status":"inactive"},{"name":"laptop-mode","status":"inactive"},{"name":"lvm2","status":"inactive"},{"name":"lvm2-lvmetad","status":"inactive"},{"name":"anacron","status":"inactive"},{"name":"network-manager","status":"inactive"},{"name":"nmbd","status":"inactive"},{"name":"plymouth","status":"inactive"},{"name":"plymouth-log","status":"inactive"},{"name":"pppd-dns","status":"inactive"},{"name":"prometheus","status":"inactive"},{"name":"rsync","status":"inactive"},{"name":"samba","status":"inactive"},{"name":"samba-ad-dc","status":"inactive"},{"name":"saned","status":"inactive"},{"name":"speech-dispatcher","status":"inactive"},{"name":"unattended-upgrades","status":"inactive"},{"name":"uuidd","status":"inactive"},{"name":"unattended-upgrades","status":"active"}]; 
 
array = array.sort(function (a,b) { 
 
    var textA = a.name.toUpperCase(); 
 
    var textB = b.name.toUpperCase(); 
 
    return (textA < textB) ? -1 : (textA > textB) ? 1 : 0; 
 
}) 
 
console.log(array.map(function(i){ 
 
    return i.name 
 
}))

+0

'a.name - b.name'總是'NaN'因爲在你的數據'name'是'字符串type',因此[不-A-號]( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN)。 – fqhv

0

使用localCompare爲按字母順序排序。

var array = [{"name":"whoopsie","status":"active"},{"name":"acpid","status":"active"},{"name":"apache2","status":"active"},{"name":"apparmor","status":"active"},{"name":"apport","status":"active"},{"name":"avahi-daemon","status":"active"},{"name":"bluetooth","status":"active"},{"name":"cron","status":"active"},{"name":"cups","status":"active"},{"name":"cups-browsed","status":"active"},{"name":"dbus","status":"active"},{"name":"grub-common","status":"active"},{"name":"irqbalance","status":"active"},{"name":"kmod","status":"active"},{"name":"lightdm","status":"active"},{"name":"lvm2-lvmetad","status":"active"},{"name":"lvm2-lvmpolld","status":"active"},{"name":"apache-htcacheclean","status":"active"},{"name":"network-manager","status":"active"},{"name":"networking","status":"active"},{"name":"postfix","status":"active"},{"name":"procps","status":"active"},{"name":"resolvconf","status":"active"},{"name":"rsyslog","status":"active"},{"name":"smartmontools","status":"active"},{"name":"smbd","status":"active"},{"name":"speech-dispatcher","status":"active"},{"name":"ssh","status":"active"},{"name":"thermald","status":"active"},{"name":"tlp","status":"active"},{"name":"udev","status":"active"},{"name":"ufw","status":"active"},{"name":"lvm2-lvmpolld","status":"inactive"},{"name":"mysql","status":"active"},{"name":"alsa-utils","status":"inactive"},{"name":"x11-common","status":"inactive"},{"name":"apache-htcacheclean","status":"inactive"},{"name":"avahi-daemon","status":"inactive"},{"name":"console-setup.sh","status":"inactive"},{"name":"cryptdisks","status":"inactive"},{"name":"cryptdisks-early","status":"inactive"},{"name":"cups-browsed","status":"inactive"},{"name":"grub-common","status":"inactive"},{"name":"hwclock.sh","status":"inactive"},{"name":"kerneloops","status":"inactive"},{"name":"keyboard-setup.sh","status":"inactive"},{"name":"laptop-mode","status":"inactive"},{"name":"lvm2","status":"inactive"},{"name":"lvm2-lvmetad","status":"inactive"},{"name":"anacron","status":"inactive"},{"name":"network-manager","status":"inactive"},{"name":"nmbd","status":"inactive"},{"name":"plymouth","status":"inactive"},{"name":"plymouth-log","status":"inactive"},{"name":"pppd-dns","status":"inactive"},{"name":"prometheus","status":"inactive"},{"name":"rsync","status":"inactive"},{"name":"samba","status":"inactive"},{"name":"samba-ad-dc","status":"inactive"},{"name":"saned","status":"inactive"},{"name":"speech-dispatcher","status":"inactive"},{"name":"unattended-upgrades","status":"inactive"},{"name":"uuidd","status":"inactive"},{"name":"unattended-upgrades","status":"active"}]; 
 
array = array.sort(function (a,b) { 
 
    return a.name.localeCompare(b.name) 
 
}) 
 
console.log(array.map(function(i){ 
 
    return i.name 
 
}))

相關問題