Orchestrator Function: vCenter Sort Array of VcManagedEntity by any Property Value

by Simon Sparks · 20 January 2026

To use this function add it to the class library file named vCenterServerService.ts

GitHub Repository: https://github.com/SimonSparksUK/Orchestrator

Filename: vCenterServerService.ts

Function VcManagedEntitySort

Description: Orchestrator Function to Sort an Array of VcManagedEntity by any Property Value

public VcManagedEntitySort<T>(arrVcManagedEntity: T[], enumSortOrder: SortOrder, strPropertyName: string = "name"): T[] {
    if (enumSortOrder === SortOrder.Forward) {

        arrVcManagedEntity = arrVcManagedEntity.sort((objVcManagedEntityA: T, objVcManagedEntityB: T): 1 | -1 | 0 => {

            if (objVcManagedEntityA[strPropertyName] < objVcManagedEntityB[strPropertyName]) {
                return -1;
            } else if (objVcManagedEntityA[strPropertyName] > objVcManagedEntityB[strPropertyName]) {
                return 1;
            } else if (objVcManagedEntityA[strPropertyName] === objVcManagedEntityB[strPropertyName]) {
                return 0;
            }
        });
    }
    else if (enumSortOrder === SortOrder.Reverse) {

        arrVcManagedEntity = arrVcManagedEntity.sort((objVcManagedEntityA: T, objVcManagedEntityB: T): 1 | -1 | 0 => {

            if (objVcManagedEntityA[strPropertyName] < objVcManagedEntityB[strPropertyName]) {
                return 1;
            } else if (objVcManagedEntityA[strPropertyName] > objVcManagedEntityB[strPropertyName]) {
                return -1;
            } else if (objVcManagedEntityA[strPropertyName] === objVcManagedEntityB[strPropertyName]) {
                return 0;
            }
        });
    }

    return arrVcManagedEntity;
}

Enumeration: SortOrder

export enum SortOrder {
  Forward = "Forward",
  Reverse = "Reverse"
}

Discover more from Cloud Build Tools

Subscribe to get the latest posts sent to your email.

You may also like