35 lines
981 B
C#
35 lines
981 B
C#
using System.Collections.Generic;
|
|
using MongoDB.Driver;
|
|
using LCU.Library.Internal.DocumentIngestorModels.Models.Pagination;
|
|
|
|
namespace LCU.Library.Internal.DocumentModels.Requests.Search;
|
|
|
|
public class Sorter<T>
|
|
{
|
|
public static SortDefinition<T> GetSorter(List<SortableField> fields)
|
|
{
|
|
SortDefinition<T>? sortDefinition = null;
|
|
|
|
if(fields == null)
|
|
{
|
|
return sortDefinition;
|
|
}
|
|
|
|
fields.Sort((a, b) => a.Position.CompareTo(b.Position));
|
|
|
|
foreach (var field in fields)
|
|
{
|
|
if (sortDefinition == null)
|
|
{
|
|
sortDefinition = field.IsDescending ? Builders<T>.Sort.Descending(field.Field) : Builders<T>.Sort.Ascending(field.Field);
|
|
}
|
|
else
|
|
{
|
|
sortDefinition = field.IsDescending ? sortDefinition.Descending(field.Field) : sortDefinition.Ascending(field.Field);
|
|
}
|
|
}
|
|
|
|
return sortDefinition;
|
|
}
|
|
}
|