我有一个包含status和name属性的对象数组。
我想使用以下代码基于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' ]
答案 0 :(得分: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
}))
&#13;
答案 1 :(得分: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
}))
&#13;