1

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?) enter image description here

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
            );
0

1 Answer 1

2

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();
Sign up to request clarification or add additional context in comments.

2 Comments

That works perfect thank you. What does out starDate do?
It's just an variable inside anonymous method as all other variables in your app. startDate used because TryParse method has out parameter and it need to be declared in code above. It used to save parsed value from array element. You can extract all code in select block to the separated method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.