简体   繁体   中英

Sort Multi Dimensional/Jagged Array from Api

I need a little help beyond what I have found on stack so far.

I have an array that looks like this (First is this Multi Dimensional or Jagged?) 在此处输入图片说明

Second I would like to Sort this by the Start Date which is [X][4]

I have tried several searches and saw the below which I tired

//string[][] SenorityList = SalesEmployees.OrderBy(inner => inner[0][4]).ToArray();

But I dont really understand how it works so cant make it work...

I also saw http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=151 which looks like it may work by using a class but again not understanding it so not sure how to deploy it for my needs.

Below I have added the export I am using that builds the array so you can see the variable names etc.

#region GrabSalesEmployees
        DateTime Now = DateTime.Now;
        EP203000Content EP203000 = context.EP203000GetSchema();
        context.EP203000Clear();
        string[][] SalesEmployees;
        SalesEmployees = context.EP203000Export(
                new Command[] {
                    EP203000.EmployeeInfo.ServiceCommands.EveryEmployeeID,
                    EP203000.GeneralInfoEmployeeSettings.EmployeeClass,
                    EP203000.EmployeeInfo.Status,
                    EP203000.EmployeeInfo.EmployeeID,
                    EP203000.EmploymentHistory.Position,
                    EP203000.EmploymentHistory.StartDate,
                    EP203000.EmploymentHistory.EndDate
                },

                new Filter[] {
                    new Filter { Field = new Field { FieldName = EP203000.GeneralInfoEmployeeSettings.EmployeeClass.FieldName }, Condition = FilterCondition.Equals, Value = "SALES", Operator = FilterOperator.And },             
                    new Filter { Field = new Field { FieldName = EP203000.EmployeeInfo.Status.FieldName }, Condition = FilterCondition.Equals, Value = "Active", Operator = FilterOperator.And },                                     
                    new Filter { Field = new Field { FieldName = EP203000.EmployeeInfo.EmployeeID.FieldName }, Condition = FilterCondition.NotEqual, Value = "BA00000450", Operator = FilterOperator.And },
                },

                    0, false, false
            );

Jagged array is an array of arrays . If you are sure that every inner array contains date in the 4th element you can use next code:

// for each element of external array (1st dimension) order by 4th element of jagged (2nd dimension) by ascending
string[][] SenorityList = SalesEmployees.OrderBy(innerArray => innerArray[4]).ToArray();

Of course the better way is to check elements and cast them to DateTime:

string[][] SenorityList = SalesEmployees.OrderBy(innerArray =>
        {
            if (innerArray.Length >= 5)
            {
                DateTime startDate;
                if (DateTime.TryParse(innerArray[4], out startDate))
                    return startDate;
            }
            // if you want that unpasrsed dates will be on the end of the list use DateTime.MaxValue
            return DateTime.MaxValue;
        }).ToArray();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM