Hi,
First off, I am new to Javascript.
I have the following javascript code, and I want to sort my HostList Array by Host name.
I tried the Array.sort(); function, but since it is objects, and not a list of hostnames. The result is done by Host name.
Does anyone know how to achieve the desired result?
/Brian
--------------------------------------
var allHosts = VcPlugin.getAllHostSystems();
HostNameToMatch = "ESXi";
var hosts = new Array();
for (var i in allHosts) {
if (allHosts[i].name.match(new RegExp(HostNameToMatch, "i"))) {
hosts.push(allHosts[i]);
}
}
HostList = hosts;
HostList.sort();
--------------------------------------
Hi Brian,
using function for extend sort ....
example down done by brain dump, so look in API explorer at Array, there is also an example
regards,
Andreas
var allHosts = VcPlugin.getAllHostSystems();
HostNameToMatch = "ESXi";
var hosts = new Array();
for (var i in allHosts) {
if (allHosts[i].name.match(new RegExp(HostNameToMatch, "i"))) {
hosts.push(allHosts[i]);
}
}
HostList = hosts;
HostList.sort(byName);
function byName(a,b)
{ return (a.name < b.name)}Hi Brian,
using function for extend sort ....
example down done by brain dump, so look in API explorer at Array, there is also an example
regards,
Andreas
var allHosts = VcPlugin.getAllHostSystems();
HostNameToMatch = "ESXi";
var hosts = new Array();
for (var i in allHosts) {
if (allHosts[i].name.match(new RegExp(HostNameToMatch, "i"))) {
hosts.push(allHosts[i]);
}
}
HostList = hosts;
HostList.sort(byName);
function byName(a,b)
{ return (a.name < b.name)}Thanks.
It works perfectly.