0

I have a list of items in a SharePoint which has Title, Date and Priority. I would like to sort this collection on my client application while displaying it ,sorting need to first by Date Descending and then within a set of objects for a date, it should sort objects by Priority.

I am using below LINQ code, which is not providing the expected result. Means the below code not sorting object by priority with in a date.

 var results = spObjects.OrderByDescending(n => n.Date).ThenBy(t => t.Priority);

Unsorted collection is as below

enter image description here

Sorted Collection - Expectation

enter image description here

I am using SharePoint CAML query, since I am fetching these object from SP and I found it difficult to do it in CAML, but I assume this can be done using LINQ on C# side, Isnt it ? Could anyone please help ?

4
  • Please include a minimal reproducible example in your question. We cannot help you without code Commented Feb 15, 2018 at 23:40
  • Possible duplicate of Multiple Order By with LINQ Commented Feb 15, 2018 at 23:42
  • Added code @maccettura Commented Feb 15, 2018 at 23:54
  • @devlincarnate - Code added. Commented Feb 15, 2018 at 23:56

2 Answers 2

0

I'll bet you that n.Date is a DateTime object, with hours, minutes, and seconds. Your Linq looks good, so you probably have different times that are causing items later in the day with lower priorities to be sorted before items earlier in the day.

Try something like:

 var results = spObjects.OrderByDescending(n => new DateTime(n.Date.Year, n.Date.Month, n.Date.Day)).ThenBy(t => t.Priority);
Sign up to request clarification or add additional context in comments.

Comments

0

If I understand you correctly you want a nested collection under a date heading, you might do something like this...

 var results = from so in spObjects
               orderby so.Date descending
               select new {
                    Date = so.Date,
                    Items = (from so2 in spObjects
                             where so2.Date == so.Date
                             orderby so2.Priority
                             select new {
                                  so2.Title,
                                  so2.Priority
                             })
                    };

4 Comments

thanks for replying, I tried this, not working. Once its is sorted by Date descending and then within that date, I need to sort by priority Ascending.
The purpose of the clause "orderby so2.Priority" is specifically to give you an ascending sort on priority in the nested query.
Yes, checked but unfortunately its not working for me. For me Priority is an Enum which has {Urgent=1, Regualar =2 } . But I dont think it makes a difference, Is it ?
No, that doesn't matter since the enum is represented as an int in the database. Can you actually check the database and make sure the priorities are set correctly? Perhaps you have a problem with the code where you are displaying enums?

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.