Home > database >  Sorting within CosmosDB based on different parameters
Sorting within CosmosDB based on different parameters

Time:02-04

I have a problem where it comes to custom sorting.

I need to sort elements based on 3 factors:

  1. ID (string)
  2. Flag (boolen)
  3. Date (datetime

Example fiddle code: https://dotnetfiddle.net/OSicuK

Sample data records:

{
            "id": "cbb440a8-fc26-4d30-966c-eba114410c6b",
            "creationDate": "2021-05-25T00:00:00",
            "child": false,
            "parentId": "cbb440a8-fc26-4d30-966c-eba114410c6b"
        },
        {
            "id": "cf70fbef-aa4f-45d3-96ab-3e211c290765",
            "creationDate": "2021-05-27T00:00:00",
            "child": true,
            "parentId": "cbb440a8-fc26-4d30-966c-eba114410c6b"
        },
        {
            "id": "48a82712-3e72-3ca7-a02d-edafd170f0b5",
            "creationDate": "2021-06-17T00:00:00",
            "child": false,
            "parentId": "48a82712-3e72-3ca7-a02d-edafd170f0b5"
        },
        {   (this one should be ealier since creationDate is ealier than one above and both child is set to false)
            "id": "1e4372f9-54dd-45b1-a401-56accbefc936", 
            "creationDate": "2021-05-25T00:00:00",
            "child": false,
            "parentId": "1e4372f9-54dd-45b1-a401-56accbefc936"
        },
        {
            "id": "cf70fbef-aa4f-45d3-96ab-3e211c290765",
            "creationDate": "2021-05-27T00:00:00",
            "child": true,
            "parentId": "1e4372f9-54dd-45b1-a401-56accbefc936"
        },
        {
            "id": "82d7ea62-8f30-4bfd-be27-eb79c0f5e9e9",
            "creationDate": "2021-05-27T00:00:00",
            "child": true,
            "parentId": "1e4372f9-54dd-45b1-a401-56accbefc936"
        },
        {
            "id": "48a82712-3e72-3ca7-a02d-edafd170f0b5",
            "creationDate": "2021-06-17T00:00:00",
            "child": true,
            "parentId": "1e4372f9-54dd-45b1-a401-56accbefc936"
        }

Records need to be sorted from oldest parent to the newest, when child flag is set to false then record needs to be before all records with the same parentId but child flag set to true.

I've tried following:

return documents .OrderBy(x => x.parentId) .ThenBy(x => x.child) .ThenBy(x => x.creationDate);

Unfortunately CreationDate is not sorted correctly.

Thanks in advance!

CodePudding user response:

What you're missing is the grouping of data.

Without grouping you can either sort on ParentId or on CreationDate, but you want to sort the parents with their children (ie grouped), in the order of the parents' CreationDate.

var results = list
    .OrderBy(x => x.CreationDate)
    .ThenBy(x => x.Child)
    .GroupBy(x => x.ParentId)
    .SelectMany(x => x);

CodePudding user response:

The issue you are facing can be resolved by changing the sorting order. You can use OrderByDescending instead of OrderBy.

Below is the sample code:-

return documents.OrderByDescending(x => x.parentId).ThenBy(x =>
 x.child).ThenBy(x => x.creationDate);
  • Related