Sorting The Data with MongoDB C# driver

Hello All,

I want to Sort the data in MongoDB collection, I am using

var res = await _dbcontext.CollectionName
               .Find(filter,new FilterOptions{Collation = new Collation 
                   ("en",caseFirst:CollationCaseFirst.Upper,strength:CollationStrength.Tertiary)})
               .Limit(limit)
               .SortBy(i=>i.Name)
               .ToListAsync(cancellationToken);

But I want the data like Upper case first in Sorted Order and then lower case in sorted order.
This query is sorting the data like

Ajay
andrew
ashish
Mark

but I want the result like

Ajay
Mark
andrew
ashish

Welcome to the community, @Himanshu_Pathak. Glad you joined us here.

Good question, and off the top of my head, I am not sure if it’s possible to get it in that sorted order because at tertiary level, base characters takes precedence over everything. You may have to query and sort mixed-case names first and then run another query and sort to get lowercase names, or just handle the sort in C# after fetching the data.

Let’s see if anyone else has a better solution.

Thanks,
Mahi