# How to Sort ripgrep Search Results by Path, Modified Time, or Match Count

> Learn how to sort ripgrep search results by path, modified time, or match count using the --sort flag and external utilities for efficient file searching.

- Repository: [Andrew Gallant/ripgrep](https://github.com/BurntSushi/ripgrep)
- Tags: how-to-guide
- Published: 2026-03-05

---

**Use the `--sort` flag with `path`, `modified`, `accessed`, or `created` arguments to order results alphabetically or by timestamp, and pipe `rg --count` to external utilities like `sort` to arrange output by match frequency.**

The `ripgrep` command-line tool provides built-in sorting capabilities that let you organize search results by file metadata rather than the default traversal order. According to the BurntSushi/ripgrep source code, the `--sort` flag defined in [`crates/core/flags/defs.rs`](https://github.com/BurntSushi/ripgrep/blob/main/crates/core/flags/defs.rs) (lines 1245–1298) maps to the `SortModeKind` enum in [`crates/core/flags/lowargs.rs`](https://github.com/BurntSushi/ripgrep/blob/main/crates/core/flags/lowargs.rs), offering deterministic ordering options that trade parallelism for sequence.

## Using the Built-in `--sort` Flag

The `SortModeKind` enum in [`crates/core/flags/lowargs.rs`](https://github.com/BurntSushi/ripgrep/blob/main/crates/core/flags/lowargs.rs) (lines 66–71) defines four sorting methods that control how the list of matching files is ordered before printing. When you specify any sort method, ripgrep switches to single-threaded traversal to preserve deterministic output, as noted in the source comments for `search_parallel` in [`crates/core/main.rs`](https://github.com/BurntSushi/ripgrep/blob/main/crates/core/main.rs) (lines 58–60).

### Sort by File Path (Alphabetical)

To sort results alphabetically by filename or full path, use the `path` method:

```bash
rg --sort path <PATTERN>

```

This maps to `SortModeKind::Path` in the source and orders files lexicographically based on their pathname. To reverse the order (Z to A), use the `--sortr` flag instead:

```bash
rg --sortr path <PATTERN>

```

### Sort by Modification Time

To display files starting with the most recently modified, use the `modified` method:

```bash
rg --sort modified <PATTERN>

```

This corresponds to `SortModeKind::LastModified` in [`lowargs.rs`](https://github.com/BurntSushi/ripgrep/blob/main/lowargs.rs). The implementation retrieves `mtime` metadata from the filesystem and sorts descending by default. Use `--sortr modified` to show oldest files first.

### Sort by Accessed or Created Time

Beyond modification time, ripgrep supports sorting by other filesystem timestamps:

- **`--sort accessed`**: Orders by last-accessed time (`SortModeKind::LastAccessed`)
- **`--sort created`**: Orders by creation time (`SortModeKind::Created`)

```bash
rg --sort accessed <PATTERN>
rg --sort created <PATTERN>

```

### Performance Impact of Sorting

Enabling any `--sort` option **disables parallelism**. The `search_parallel` function in [`crates/core/main.rs`](https://github.com/BurntSushi/ripgrep/blob/main/crates/core/main.rs) explicitly notes that sorting forces single-threaded execution because results must be collected and ordered before display. If search speed is critical and you do not need deterministic ordering, omit the `--sort` flag to allow multi-threaded directory traversal.

## Sorting by Match Count

ripgrep does not provide a native "sort by match count" mode. Instead, use the `-c`/`--count` flag to output `<filename>:<count>` pairs, then pipe to GNU `sort` for numeric ordering:

```bash
rg -c <PATTERN> | sort -t: -k2 -n -r

```

This command breaks down as follows:

- `-c` prints the count of matches per file
- `sort -t:` uses colon as the field delimiter
- `-k2` selects the second field (the numeric count)
- `-n` treats the field as numeric rather than lexical
- `-r` reverses the order to show highest counts first

To retrieve the actual matching lines after determining the sort order, chain the commands:

```bash
rg -c <PATTERN> \
  | sort -t: -k2 -n -r \
  | cut -d: -f1 \
  | xargs -I{} rg <PATTERN> {}

```

## Platform Limitations and Error Handling

Not all platforms support every timestamp type. The validation logic in [`crates/core/flags/lowargs.rs`](https://github.com/BurntSushi/ripgrep/blob/main/crates/core/flags/lowargs.rs) (lines 80–88) produces an error like `sorting by last created isn't supported` when the underlying filesystem or operating system cannot provide the requested metadata. If you encounter this error, fall back to `--sort path` or `--sort modified`, which are universally supported.

Additionally, remember that sorting requires buffering all results in memory to establish order. For extremely large result sets, consider using `--max-files` or `--max-count` to limit the dataset before sorting.

## Summary

- **Built-in sorting**: Use `--sort path|modified|accessed|created` for deterministic ordering based on file metadata.
- **Reverse sorting**: Append the `r` suffix (`--sortr`) to invert any sort order.
- **Performance trade-off**: All `--sort` options force single-threaded execution as implemented in [`crates/core/main.rs`](https://github.com/BurntSushi/ripgrep/blob/main/crates/core/main.rs).
- **Match counts**: Pipe `rg -c` to `sort -t: -k2 -n` since ripgrep has no native "sort by count" feature.
- **Platform constraints**: Creation and access timestamps may be unavailable on some systems, raising errors defined in [`crates/core/flags/lowargs.rs`](https://github.com/BurntSushi/ripgrep/blob/main/crates/core/flags/lowargs.rs).

## Frequently Asked Questions

### Does ripgrep support sorting by match count?

No, ripgrep does not include a built-in mode to sort files by the number of matches. You must use the `-c` flag to emit counts, then pipe the output to an external tool like `sort -n` to arrange results numerically. This limitation is inherent to the `SortModeKind` enum, which only defines path and timestamp variants.

### Why does `--sort` slow down my searches?

Sorting disables parallelism. According to the source code in [`crates/core/main.rs`](https://github.com/BurntSushi/ripgrep/blob/main/crates/core/main.rs), the `search_parallel` function falls back to single-threaded traversal when any `--sort` option is active because results must be collected and ordered before display. This ensures deterministic output but sacrifices the multi-threaded performance that makes ripgrep fast.

### What timestamp sorting options are available in ripgrep?

The tool supports four sorting methods defined in [`crates/core/flags/lowargs.rs`](https://github.com/BurntSushi/ripgrep/blob/main/crates/core/flags/lowargs.rs): `path` (alphabetical), `modified` (mtime), `accessed` (atime), and `created` (birth time). Availability of `accessed` and `created` depends on your operating system and filesystem capabilities; attempting to use them on unsupported platforms triggers a validation error.

### How do I reverse the sort order in ripgrep?

Use the `--sortr` flag (note the trailing `r`) instead of `--sort`. This works with any valid sort method (`path`, `modified`, `accessed`, `created`) to invert the ordering (e.g., oldest files first instead of newest, or reverse alphabetical).