Regarding Sorting of Elements in an Array

Appu2
Tera Contributor
I have written below script include which is used to parse the values as drop downs on the front end form. The issue is I want to get the elements present in envValuesMap and roleValuesMap array to appear in alphabetical order on the front end form. But I tried couple of ways but nothing worked!
 
Any inputs here?
 
var targetTable = 'u_ntaap_mapping';
        var initialField = 'u_user_access_type';
        var otherFields = ['u_project_name', 'u_project_code', 'u_environment_team_name', 'u_environment_teams_code'];
        var initialGlideAggregate = new GlideAggregate(targetTable);
        initialGlideAggregate.addEncodedQuery('u_ntaap_version=2.0^u_active=true');
        initialGlideAggregate.addNotNullQuery(initialField);
        initialGlideAggregate.groupBy(initialField);
        initialGlideAggregate.query();
        var uniqueValuesMap = {};
        var initialValuesArray = [];
        var roleValuesMap = {};
        var envValuesMap = {};
        while (initialGlideAggregate.next()) {
            var initialValue = initialGlideAggregate.getValue(initialField);
            if (initialValuesArray.indexOf(initialValue.toString()) === -1) {
                initialValuesArray.push(initialValue.toString());
            }
            var uniqueValuesForOtherFields = {};
            for (var i = 0; i < otherFields.length; i++) {
                var otherField = otherFields[i];
                var otherFieldValues = [];
                var otherGlideRecord = new GlideRecord(targetTable);
                otherGlideRecord.addEncodedQuery('u_ntaap_version=2.0^u_active=true');
                otherGlideRecord.addQuery(initialField, initialValue);
                otherGlideRecord.query();
                while (otherGlideRecord.next()) {
                    var otherValue = otherGlideRecord.getValue(otherField);
                    if (otherValue && otherFieldValues.indexOf(otherValue.toString()) === -1) {
                        otherFieldValues.push(otherValue.toString());
                       
                    }
                    if (otherField === "u_project_code") {
                        var recordEnvName = otherGlideRecord.getValue("u_environment_team_name");
                        var recordEnvCode = otherGlideRecord.getValue("u_environment_teams_code");
                        if (Object.keys(envValuesMap).indexOf(otherValue.toString()) !== -1) {
                            envValuesMap[otherValue.toString()].push({
                                u_environment_team_name: recordEnvName,
                                u_environment_teams_code: recordEnvCode
                            });
                        } else {
                            envValuesMap[otherValue.toString()] = [{
                                u_environment_team_name: recordEnvName,
                                u_environment_teams_code: recordEnvCode
                            }];
                        }
                    }
                    if (otherField === "u_environment_teams_code") {
                        var recordProjCode = otherGlideRecord.getValue("u_project_code");
                        var recordRoleName = otherGlideRecord.getValue("u_role_name");
                        var recordRoleCode = otherGlideRecord.getValue("u_role_code");
                        if (Object.keys(roleValuesMap).indexOf(recordProjCode+"@"+otherValue.toString()) !== -1) {
                            roleValuesMap[recordProjCode+"@"+otherValue.toString()].push({
                                u_role_name: recordRoleName,
                                u_role_code: recordRoleCode
                            });
                        } else {
                            roleValuesMap[recordProjCode+"@"+otherValue.toString()] = [{
                                u_role_name: recordRoleName,
                                u_role_code: recordRoleCode
                            }];
                        }



                    }
                    uniqueValuesForOtherFields[otherField] = otherFieldValues;
                }
            }
            uniqueValuesMap[initialValue] = uniqueValuesForOtherFields;
            var uniqueValueMapping = JSON.stringify(uniqueValuesMap, null, 2);
        }
        var finalvalueArray = {
            initialValuesArray: initialValuesArray,
            uniqueValuesMap: uniqueValuesMap,
            envValuesMap: envValuesMap,
            roleValuesMap: roleValuesMap
        };
        return JSON.stringify(finalvalueArray, null, 2);



    },

 

    type: 'KPMG_nTAAPDetails'

 

});
2 REPLIES 2

Maik Skoddow
Tera Patron
Tera Patron

Hi @Appu2 

please refer to the following article to learn how to sort arrays of objects: https://www.scaler.com/topics/javascript-sort-an-array-of-objects/ 

Maik

I tried using .sort() but for the 2 arrays which I mentioned I'm not getting how to add it for them