VMware Cloud Community
Mnemonic
Enthusiast
Enthusiast
Jump to solution

HOW TO: Sort array of VC:HostSystems by Name

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();

--------------------------------------

Reply
0 Kudos
1 Solution

Accepted Solutions
Andreas_Diemer
Enthusiast
Enthusiast
Jump to solution

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)
}
------ for correct and / or useful answers please award points visit http://www.vcoteam.info & http://mighty-virtualization.blogspot.com

View solution in original post

Reply
0 Kudos
2 Replies
Andreas_Diemer
Enthusiast
Enthusiast
Jump to solution

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)
}
------ for correct and / or useful answers please award points visit http://www.vcoteam.info & http://mighty-virtualization.blogspot.com
Reply
0 Kudos
Mnemonic
Enthusiast
Enthusiast
Jump to solution

Thanks. Smiley Happy

It works perfectly.

Reply
0 Kudos