Skip to content

Commit adaa0b2

Browse files
committed
ADDED: MultiSortExtensions to allow easy sorting across multiple fields
1 parent 6811958 commit adaa0b2

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
namespace Nest
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
6+
/// <summary>
7+
/// Provides MultiSort extensions that allow easy sorting across multiple fields
8+
/// </summary>
9+
public static class MultiSortExtensions
10+
{
11+
public static SearchDescriptor<T> MultiSort<T>(
12+
this SearchDescriptor<T> instance,
13+
params Func<SortFieldDescriptor<T>, IFieldSort>[] sorts) where T : class
14+
{
15+
foreach (var sort in sorts)
16+
{
17+
instance.Sort(sort);
18+
}
19+
20+
return instance;
21+
}
22+
23+
public static SearchDescriptor<T> MultiSort<T>(
24+
this SearchDescriptor<T> instance,
25+
IEnumerable<Func<SortFieldDescriptor<T>, IFieldSort>> sorts) where T : class
26+
{
27+
foreach (var sort in sorts)
28+
{
29+
instance.Sort(sort);
30+
}
31+
32+
return instance;
33+
}
34+
35+
public static SearchDescriptor<T> MultiSort<T>(
36+
this SearchDescriptor<T> instance,
37+
IEnumerable<SortFieldDescriptor<T>> sorts) where T : class
38+
{
39+
foreach (var sort in sorts)
40+
{
41+
var copy = sort;
42+
instance.Sort(s => copy);
43+
}
44+
45+
return instance;
46+
}
47+
}
48+
}

Diff for: src/Nest/Nest.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<Compile Include="ConvenienceExtensions\DeleteManyExtensions.cs" />
7373
<Compile Include="ConvenienceExtensions\GetManyExtensions.cs" />
7474
<Compile Include="ConvenienceExtensions\CreateIndexExtensions.cs" />
75+
<Compile Include="ConvenienceExtensions\MultiSortExtensions.cs" />
7576
<Compile Include="ConvenienceExtensions\OpenCloseIndexExtensions.cs" />
7677
<Compile Include="ConvenienceExtensions\ExistsExtensions.cs" />
7778
<Compile Include="ConvenienceExtensions\ScrollExtensions.cs" />

0 commit comments

Comments
 (0)