---
title: "Server-wide Custom Sorters"
description: "Learn how to add, delete, and test server-wide custom sorters in RavenDB — sorters that can be used to sort query results in any database on the server."
url: "https://docs.ravendb.net/7.2/querying/sorting-query-results/custom-sorters/server-wide-custom-sorters"
version: "7.2"
language: "csharp"
---

# Server-wide Custom Sorters

> Client language: C#. Also available for [Node.js](https://docs.ravendb.net/7.2/querying/sorting-query-results/custom-sorters/server-wide-custom-sorters.nodejs.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

> **Note**
>
> - A server-wide custom sorter can be used to sort query results in all databases across your cluster.  
>   To learn how to define and deploy a database-level custom sorter, see [Database-level custom sorters](https://docs.ravendb.net/7.2/querying/sorting-query-results/custom-sorters/database-level-custom-sorters.md).
>
> - If a database-level custom sorter and a server-wide custom sorter have the **same name**,  
>   the database-level custom sorter will be used for the query.
>
> - Custom sorters are available only when using the **Lucene** indexing engine; they are not available with [Corax](https://docs.ravendb.net/7.2/indexes/search-engine/corax.md).  
>   To learn how to write a custom sorter, see: [How to write a custom sorter](https://docs.ravendb.net/7.2/querying/sorting-query-results/custom-sorters/overview.md#how-to-write-a-custom-sorter).
>
> - In this article:
>   - [Add server-wide custom sorter - via the Client API](#add-server-wide-custom-sorter-via-the-client-api)
>   - [Add server-wide custom sorter - via Studio](#add-server-wide-custom-sorter-via-studio)
>   - [Delete server-wide custom sorter](#delete-server-wide-custom-sorter)
>   - [Test custom sorter](#test-custom-sorter)
>   - [Syntax](#syntax)

## Add server-wide custom sorter - via the Client API {#add-server-wide-custom-sorter-via-the-client-api}

Use `PutServerWideSortersOperation` to deploy one or more server-wide custom sorters.  
Deploying a custom sorter with an existing name replaces the previous version.  
Once deployed, you can use it to sort query results in queries made on **any database** in your cluster.

### Sync

```csharp
// Assign the code of your custom sorter as a `string`
string mySorterCode = "<code of custom sorter>";

// Create the `SorterDefinition` object
var customSorterDefinition = new SorterDefinition
{
    // The sorter name must be the same as the sorter's class name in your code
    Name = "MyServerWideCustomSorter",
    
    // The code must be compilable and include all necessary using statements
    Code = mySorterCode
};

// Define the put sorters operation, pass the sorter definition
// Note: multiple sorters can be passed, see syntax below
var putSortersServerWideOp = new PutServerWideSortersOperation(customSorterDefinition);
 
// Execute the operation by passing it to Maintenance.Server.Send
store.Maintenance.Server.Send(putSortersServerWideOp);
```

### Async

```csharp
// Assign the code of your custom sorter as a `string`
string mySorterCode = "<code of custom sorter>";

// Create the `SorterDefinition` object
var customSorterDefinition = new SorterDefinition
{
    // The sorter name must be the same as the sorter's class name in your code
    Name = "MyServerWideCustomSorter",
    
    // The code must be compilable and include all necessary using statements
    Code = mySorterCode
};

// Define the put sorters operation, pass the sorter definition
// Note: multiple sorters can be passed, see syntax below
var putSortersServerWideOp = new PutServerWideSortersOperation(customSorterDefinition);
 
// Execute the operation by passing it to Maintenance.Server.SendAsync
await store.Maintenance.Server.SendAsync(putSortersServerWideOp);
```

You can now order query results using the server-wide custom sorter on any database.

### Query

```csharp
List<Product> products = session
    .Query<Product>()
    .Where(x => x.UnitsInStock > 10)
     // Order by field 'UnitsInStock', pass the name of your custom sorter class
    .OrderBy(x => x.UnitsInStock, "MyServerWideCustomSorter")
    .ToList();

// Results will be sorted by the 'UnitsInStock' value
// according to the logic from 'MyServerWideCustomSorter' class
```

### Query\_async

```csharp
List<Product> products = await asyncSession
    .Query<Product>()
    .Where(x => x.UnitsInStock > 10)
     // Order by field 'UnitsInStock', pass the name of your custom sorter class
    .OrderBy(x => x.UnitsInStock, "MyServerWideCustomSorter")
    .ToListAsync();

// Results will be sorted by the 'UnitsInStock' value
// according to the logic from 'MyServerWideCustomSorter' class
```

### DocumentQuery

```csharp
List<Product> products = session.Advanced
    .DocumentQuery<Product>()
    .WhereGreaterThan(x => x.UnitsInStock, 10)
     // Order by field 'UnitsInStock', pass the name of your custom sorter class
    .OrderBy(x => x.UnitsInStock, "MyServerWideCustomSorter")
    .ToList();

// Results will be sorted by the 'UnitsInStock' value
// according to the logic from 'MyServerWideCustomSorter' class
```

### DocumentQuery\_async

```csharp
List<Product> products = await asyncSession.Advanced
    .AsyncDocumentQuery<Product>()
    .WhereGreaterThan(x => x.UnitsInStock, 10)
     // Order by field 'UnitsInStock', pass the name of your custom sorter class
    .OrderBy(x => x.UnitsInStock, "MyServerWideCustomSorter")
    .ToListAsync();

// Results will be sorted by the 'UnitsInStock' value
// according to the logic from 'MyServerWideCustomSorter' class
```

### RawQuery

```csharp
List<Product> products = session.Advanced
     // Order by field 'UnitsInStock' with custom(), pass the name of your custom sorter class
    .RawQuery<Product>("from 'Products' where UnitsInStock > 10 order by custom(UnitsInStock, 'MyServerWideCustomSorter')")
    .ToList();

// Results will be sorted by the 'UnitsInStock' value
// according to the logic from 'MyServerWideCustomSorter' class
```

### RawQuery\_async

```csharp
List<Product> products = await asyncSession.Advanced
     // Order by field 'UnitsInStock' with custom(), pass the name of your custom sorter class
    .AsyncRawQuery<Product>("from 'Products' where UnitsInStock > 10 order by custom(UnitsInStock, 'MyServerWideCustomSorter')")
    .ToListAsync();

// Results will be sorted by the 'UnitsInStock' value
// according to the logic from 'MyServerWideCustomSorter' class
```

### RQL

```sql
from "Products"
where UnitsInStock > 10
order by custom(UnitsInStock, "MyServerWideCustomSorter")
    
// Results will be sorted by the 'UnitsInStock' value
// according to the logic from 'MyServerWideCustomSorter' class    
```

## Add server-wide custom sorter - via Studio {#add-server-wide-custom-sorter-via-studio}

### The server-wide custom sorters view

![Server-wide custom sorter view](https://docs.ravendb.net/assets/images/custom-sorter-server-wide-1-0c12bb449902cac63cf2423b8ca0da33.png)

1. Go to **Manage Server > Server-Wide Sorters**.
2. Click to add a new server-wide custom sorter.
   See [Add a server-wide custom sorter](#add-a-server-wide-custom-sorter) below.
3. The **server-wide** custom sorters are listed here.
4. Click to edit this server-wide custom sorter.
5. Click to delete this server-wide custom sorter.

---

### Add a server-wide custom sorter

![Add server-wide custom sorter](https://docs.ravendb.net/assets/images/custom-sorter-server-wide-2-526fbb5aba4eb86a66e46ac69605abbd.png)

1. The **sorter name** must be the same as the class name of your sorter.
2. You can load the sorter's code from a `*.cs` file.
3. Or, you can enter the code manually in this editor.  
   The sorter code must be compilable and include all necessary `using` statements.  
   The sorter's class should inherit from [Lucene.Net.Search.FieldComparator](https://lucenenet.apache.org/docs/3.0.3/df/d91/class_lucene_1_1_net_1_1_search_1_1_field_comparator.html).  
   Learn more in [How to write a custom sorter](https://docs.ravendb.net/7.2/querying/sorting-query-results/custom-sorters/overview.md#how-to-write-a-custom-sorter).
4. Save your server-wide sorter or discard.

## Delete server-wide custom sorter {#delete-server-wide-custom-sorter}

In addition to deleting a server-wide custom sorter via [The server-wide custom sorters view](#the-server-wide-custom-sorters-view) in Studio,  
you can use `DeleteServerWideSorterOperation` to delete a server-wide custom sorter.  
Once removed, the sorter will no longer be available for ordering query results in any database in the cluster.

### Sync

```csharp
// Define the delete sorter operation, pass the name of the sorter to delete
var deleteSorterOp = new DeleteServerWideSorterOperation("MyServerWideCustomSorter");

// Execute the operation by passing it to Maintenance.Server.Send
store.Maintenance.Server.Send(deleteSorterOp);
```

### Async

```csharp
// Define the delete sorter operation, pass the name of the sorter to delete
var deleteSorterOp = new DeleteServerWideSorterOperation("MyServerWideCustomSorter");

// Execute the operation by passing it to Maintenance.Server.SendAsync
await store.Maintenance.Server.SendAsync(deleteSorterOp);
```

## Test custom sorter {#test-custom-sorter}

To test a server-wide sorter, go to the Custom Sorters view in a specific database and use the test option from there.  
See [Test custom sorter](https://docs.ravendb.net/7.2/querying/sorting-query-results/custom-sorters/database-level-custom-sorters.md#test-custom-sorter).

## Syntax {#syntax}

### `PutServerWideSortersOperation`

Deploys one or more custom sorters to the server-wide scope.

```csharp
public PutServerWideSortersOperation(params SorterDefinition[] sortersToAdd)
```

| Parameter | Type | Description |
| - | - | - |
| **sortersToAdd** | `SorterDefinition[]` | One or more sorter definitions to deploy. At least one is required. |

```csharp
public sealed class SorterDefinition
{
    // The sorter's name. Must match the class name in the source code. 
    public string Name { get; set; }
    
    // The complete C# source code of the sorter, including all `using` statements. 
    // Must be compilable. 
    public string Code { get; set; }
}
```

---

### `DeleteServerWideSorterOperation`

Removes a custom sorter from the server-wide scope.

```csharp
public DeleteServerWideSorterOperation(string sorterName)
```

| Parameter | Type | Description |
| - | - | - |
| **sorterName** | `string` | The name of the sorter to remove. |

## See also

- [Query Overview](https://docs.ravendb.net/7.2/querying/overview.md)
- [Sort Query Results](https://docs.ravendb.net/7.2/querying/sorting-query-results/sort-query-results.md)
- [What are operations](https://docs.ravendb.net/7.2/client-api/operations/what-are-operations.md)
