š¢ stewbeet.plugins.datapack.sorters
š Source Code: stewbeet/plugins/datapack/sorters/__init__.py š
š Source Code: stewbeet/plugins/datapack/sorters/constants.py š
š Source Code: stewbeet/plugins/datapack/sorters/extend_datapack.py š
š Source Code: stewbeet/plugins/datapack/sorters/match.py š
š Source Code: stewbeet/plugins/datapack/sorters/quick_sort.py š
š Source Code: stewbeet/plugins/datapack/sorters/selection_sort.py š
š» Credits
Author: Darukshock - Original implementation of the quick sort algorithm
š Overview
The datapack.sorters plugin generates efficient sorting functions for Minecraft datapacks.
It creates optimized mcfunction files at compile time that can sort lists stored in NBT storage
using configurable algorithms (Quick Sort and Selection Sort), with support for custom comparison
keys, scaling factors, and performance optimizations tailored for Minecraft's execution environment.
š Dependencies
- ā Required: Beet context with datapack namespace support
- š Position: Can run at any point during compilation
- š§ Extension: Requires
stewbeet.plugins.datapack.sorters.extend_datapackto register sorter namespace
Quick Example
Sorters are declared in the sorters registry under data/<namespace>, like so:
data/<namespace>/sorter/<sorter_name>.json

Alternatively, they can be generated with python:

The list can now be sorted:

šÆ Purpose
- š Generate high-performance sorting functions for NBT storage lists
- šÆ Support multiple sorting algorithms optimized for Minecraft's execution model
- š Handle complex data structures with configurable comparison keys
- ā” Provide in-place sorting with minimal memory overhead
- š§ Enable precision control through scaling for decimal values
- š Support partial sorting with element limits for performance optimization
āļø Configuration
š Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
algorithm | string | Required | Sorting algorithm to use: "selection_sort" (recommended) or "quick_sort" |
functions_location | string | Required | Namespaced function ID where sorting functions are generated |
to_sort | object | Required | Storage location containing storage and target keys |
to_sort.storage | string | Required | Storage namespace (e.g., "switch:stats") |
to_sort.target | string | Required | NBT path to the list (e.g., "all.modes.sheepwars.played") |
key | string | Required | Key within each list element to compare for sorting |
scale | float | 1.0 | Optional: Scaling factor for numeric values (negative for descending order) |
limit | integer | null | Optional: Maximum elements to sort (selection_sort only) |
┠Performance Note: Selection sort significantly outperforms quick sort in Minecraft's storage environment due to quick sort's heavy reliance on macro expansions. Each recursive call in quick sort generates multiple macro invocations for parameter passing and storage manipulation, creating substantial overhead that negates the theoretical O(n log n) advantage. Selection sort's straightforward O(n²) approach with minimal macro usage proves more efficient for typical Minecraft datapack sorting operations.
šÆ Basic Example Configuration
require: - ... - stewbeet.plugins.datapack.sorters.extend_datapack # Required: register "data/<namespace>/sorter/" as a custom resource - ... pipeline: - ... - stewbeet.plugins.datapack.sorters # Place this plugin in the pipeline - ... # Other plugins follow
š Example Sorter Configuration
{ "algorithm": "selection_sort", "functions_location": "switch:stats/minigame/sort_leaderboard", "to_sort": { "storage": "switch:stats", "target": "all.modes.sheepwars.played" }, "key": "count", "scale": 100, "limit": 10 }
š Spyglass implementation
Combined with the Spyglass extension for Visual Studio Code, auto-completion and syntax checking can be obtained on sorter files.
Here's how:
- If you haven't already, open your project on Visual Studio Code and install the Spyglass extension.
- Create a Spyglass Config File, and under the
envfield, add the following :
"customResources": { "sorter": { "category": "sorter" } }
- Anywhere in your workspace, create
sorter.mcdoc: stewbeet/plugins/datapack/sorters/mod.mcdoc š
Note: it will work no matter the name as long as it ends in.mcdoc - Restart Visual Studio Code
⨠Features
šļø Algorithm Selection System
- š„ Selection Sort - Recommended for better Minecraft performance
- ā” Quick Sort - Alternative using recursive function calls
- š Automatic Routing - Algorithm selection based on configuration
š¦ Extended Datapack Support
- šļø Custom Resource Type - Sorter objects at
data/<namespace>/sorter/path.json - š Spyglass Support - Syntax checking and auto-completion
- ā Multi-Datapack Support - Independent configurations
š¢ Advanced Scaling System
- š Precision Control - Scale factor for decimal precision (e.g.,
1000for 3 decimals) - š Reverse Sorting - Negative scale values for descending order
ā” Selection Sort Implementation
- š Minimum Finding - Efficient linear search
- š Partial Sorting - Optional limit parameter for top-N sorting
- š¾ Memory Efficient - In-place sorting with minimal storage overhead
š Quick Sort Implementation
- š Pivot Selection - Last element partitioning
- š Recursive Calls - Function-based recursion with macros
- ā ļø Performance Note - Less efficient than selection sort due to macro overhead
š Configuration & Generation
- ā Input Validation - Type checking and required field validation
- ļæ½ Function Generation - Dynamic mcfunction file creation with macro integration
- ļæ½ Memory Management - Temporary storage with automatic cleanup