首页 > 解决方案 > 根据不同数组的 array.sort() 对一个数组进行排序

问题描述

下面的代码有两个功能。第一个功能是创建对象。第二个函数是验证标头,并创建一个字符串数组,该数组将在第一个函数中用于将值映射到对象属性 (arrOfIndices)。

//Global variable
var arrOfIndices;

function createFileObjects(allText) {

    validateHeaders(allText[0]);

    var fileMap = {}

    for(var i = 1; i < allText.length; i++) {

            var currentParams = allText[i].split(",");
            for (var j = 0; j < arrOfIndices.length; j++) {
                fileMap[arrOfIndices[j]] = currentParams[j];
            }
    }

}

function validateHeaders(headerString) {
    var headerPermutations = function (sealPerms, appNamePerms, entityIdPerms, entityNamePerms, entityTypePerms, envPerms, logIdPerms, sidPerms, userNamePerms, userStatusPerms, entlNamePerms, entlDescPerms, fidOwnerSidPerms, fidOwnerNamePerms, lmSidPerms, lmNamePerms) {
        this.sealPerms = sealPerms;
        this.appNamePerms = appNamePerms;
        this.entityIdPerms = entityIdPerms;
        this.entityNamePerms = entityNamePerms;
        this.entityTypePerms = entityTypePerms;
        this.envPerms = envPerms;
        this.logIdPerms = logIdPerms;
        this.sidPerms = sidPerms;
        this.userNamePerms = userNamePerms;
        this.userStatusPerms = userStatusPerms;
        this.entlNamePerms = entlNamePerms;
        this.entlDescPerms = entlDescPerms;
        this.fidOwnerSidPerms = fidOwnerSidPerms;
        this.fidOwnerNamePerms = fidOwnerNamePerms;
        this.lmSidPerms = lmSidPerms;
        this.lmNamePerms = lmNamePerms;
    }


    var seal = ['seal', 'id', 'seel', 'identification', 'app', 'application', 'identifier'];
    var appName = ['app', 'application', 'appl', 'applic', 'name', 'nm'];
    var entId = ['entity', 'ent', 'id', 'identifier'];
    var entName = ['entity', 'ent', 'name'];
    var entType = ['entity', 'ent', 'type'];
    var env = ['env', 'environment'];
    var logId = ['log', 'login', 'id', 'identifier'];
    var sid = ['sid', 'standard', 'id'];
    var userName = ['username', 'user', 'name', 'employee'];
    var userStat = ['status', 'user', 'employee', 'activity', 'active'];
    var entlName = ['entitlement', 'entl', 'name'];
    var entlDesc = ['entitlement', 'entl', 'description', 'desc'];
    var fidSid = ['fid', 'owner', 'sid', 'functional', 'id', 'standard'];
    var fidName = ['fid', 'owner', 'function', 'id', 'name'];
    var lmSid = ['lm', 'line', 'manager', 'sid', 'standard', 'id', 'identifier'];
    var lmName = ['lm', 'line', 'manager', 'name'];

    arrOfPermutations.push(seal, appName, entId, entName, entType, env, logId, sid, userName, userStat, entlName, entlDesc, fidSid, fidName, lmSid, lmName);
    var arrOfBools = [];
    var sortedArr = string.split(",").sort;    


    for (var i = 0; i < sortedArr.length; i++) {
        for (var j = 0; j < arrOfPermutations.length; j++) {
            var booleanTest = match(sortedArr[i], arrOfPermutations[j]);
            if (booleanTest) {
                switch (j) {
                    case 0: arrOfIndices.push("sealId");
                    break;

                    case 1: arrOfIndices.push("applicationNameInSeal");
                    break;

                    case 2: arrOfIndices.push("entityId");
                    break;

                    case 3: arrOfIndices.push("entityName");
                    break;

                    case 4: arrOfIndices.push("entityType");
                    break;

                    case 5: arrOfIndices.push("environment");
                    break;

                    case 6: arrOfIndices.push("loginId");
                    break;

                    case 7: arrOfIndices.push("userSid");
                    break;

                    case 8: arrOfIndices.push("userName");
                    break;

                    case 9: arrOfIndices.push("userStatus");
                    break;

                    case 10: arrOfIndices.push("userType");
                    break;

                    case 11: arrOfIndices.push("entitlementName");
                    break;

                    case 12: arrOfIndices.push("entitlementDesc");
                    break;

                    case 13: arrOfIndices.push("fidOwnerSid");
                    break;

                    case 14: arrOfIndices.push("fidOwnerName");
                    break;

                    case 15: arrOfIndices.push("lmSid");
                    break;

                    case 16: arrOfIndices.push("lmName");
                    break;
                }

                arrOfBools.push(true);
            }
        }

    }

    if (arrOfBools.length == sortedArr.length) {
        //return arrOfIndices
        return "File Format Correct";
    } else { return "File Format Incorrect";}
}

预期结果是对象根据已排序的 headerArray 正确映射。

标签: javascriptarrayssorting

解决方案


如果有人好奇,我想出了一个答案。(也许它可以帮助别人?)

for (var i = 0; i < originalArr.length; i++) {
        for (var j = 0; j < sortedArr.length; j++) {
            if (originalArr[i] == sortedArr[j]) {
                arrOfIndexes.push(j);
            }

        }
    }

    var fileMap = {}
    var currentSorted = [];

    for (k = 0; k < allText.length; k++) {
            currentSorted.push(' ');
        }

    for (var i = 1; i < allText.length; i++) {
        var currentParams = allText[i].split(",");
        for (var j = 0; j < currentParams.length; j++) {

            currentSorted[arrOfIndexes[j]] = currentParams[j];

        }
        allTextSorted.push(currentSorted.join());
    }

推荐阅读