Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@burkeazbill
Created March 9, 2017 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burkeazbill/4769265f8d8e25c8029a8ae895d06683 to your computer and use it in GitHub Desktop.
Save burkeazbill/4769265f8d8e25c8029a8ae895d06683 to your computer and use it in GitHub Desktop.
sortDatastores.js
/*
Authors: Burke Azbill & Bill Call (2011)
Name: sortDatastores.action
Description: This script is intended to be used as a vRealize Orchestrator Action. It will return an array of sorted VC:Datastore objects,
sorted as defined by the sortType input (freeSpace, freeSpaceDesc, capacity, capacityDesc, uncommitted, uncommittedDesc, nameDesc)
where "Desc" means Descending. If no Sort Type is defined, then the default sort is by Name.
This code also serves as an example on how to sort arrays of Objects in vRealize Orchestrator
Inputs: datastores (Array/VC:Datastore), sortType (string), logResults (boolean)
Output: Array/VC:Datastore
Code:
*/
System.log("==== Beginning to sort: "+sortType+" ====");
var ds = new Array();
for each (d in datastores){
ds.push(d);
}
switch (sortType){
case ("freeSpace"):
ds.sort(sortByFreeSpace);
break;
case ("freeSpaceDesc"):
ds.sort(sortByFreeSpaceDesc);
break
case ("capacity"):
ds.sort(sortByCapacity);
break;
case ("capacityDesc"):
ds.sort(sortByCapacityDesc);
break
case ("uncommitted"):
ds.sort(sortByUncommitted);
break;
case ("uncommittedDesc"):
ds.sort(sortByUncommittedDesc);
break
case ("nameDesc"):
ds.sort(sortByNameDesc);
break;
default: /* if sortType not specified default to sorting by name */
ds.sort(sortByName);
break;
}
if (logResults){
System.log("Sorted DS List: (Name: freeSpace, capacity, uncommitted)");
for each (datastore in ds){
System.log(datastore.name + ": "+datastore.summary.freeSpace+", "+datastore.summary.capacity+", "+datastore.summary.uncommitted);
}
}
return ds;
/* FreeSpace Sorting */
function sortByFreeSpace(a,b){
return a.summary.freeSpace - b.summary.freeSpace;
}
function sortByFreeSpaceDesc(a,b){
return b.summary.freeSpace - a.summary.freeSpace;
}
/* Capacity Sorting */
function sortByCapacity(a,b){
return a.summary.capacity - b.summary.capacity;
}
function sortByCapacityDesc(a,b){
return b.summary.capacity - a.summary.capacity;
}
/* Uncommitted Sorting */
function sortByUncommitted(a,b){
return a.summary.uncommitted - b.summary.uncommitted;
}
function sortByUncommittedDesc(a,b){
return b.summary.uncommitted - a.summary.uncommitted;
}
/* Name Sorting */
function sortByName(a,b){
var dsA = a.name;
var dsB = b.name;
if(dsA == dsB){
return 0;
}else if(dsA==null){
return 1;
}else if(dsB==null){
return -1;
}else{
var dsALength = dsA.length;
var dsBLength = dsB.length;
if(dsALength > dsBLength){
var maxLength = dsALength;
}else{
var maxLength = dsBLength;
}
for(var i=0; i<maxLength; i++){
if(dsA.charAt(i) != dsB.charAt(i)){
if(dsA.charAt(i) < dsB.charAt(i)){
return -1;
}else{
return 1;
}
}
}
}
}
function sortByNameDesc(a,b){
var dsA = a.name;
var dsB = b.name;
if(dsA == dsB){
return 0;
}else if(dsA==null){
return 1;
}else if(dsB==null){
return -1;
}else{
var dsALength = dsA.length;
var dsBLength = dsB.length;
if(dsALength < dsBLength){
var maxLength = dsALength;
}else{
var maxLength = dsBLength;
}
for(var i=0; i<maxLength; i++){
if(dsA.charAt(i) != dsB.charAt(i)){
if(dsA.charAt(i) > dsB.charAt(i)){
return -1;
}else{
return 1;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment