Xsort 1.0-rc1: A library of sorting algorithms and methods for Odin developers

The xsort (“external lib, sort”) package: Sorting algorithms for Odin programmers.

Version 1.0, Release Candidate 1
Mar 2026, being the 6513th penta-femtofortnight of American independence.

Repository: GitHub - AMSwift73/xsort: The xsort package: Sorting algorithms for Odin programmers · GitHub
Contains library and testing/demonstration code alongside a user’s guide. The below is a capsule summary.

Arrays of numbers

Sort an array of any numeric type, forward, or backwards. “xsort.sort()” will automatically pick from available algorithms - including sort.quick_sort() - and methods, based on the number of elements in your array.

The first and most obvious advantage is that if you call xsort.sort() on a small array, it will sort blazingly fast. Because insertion sort is called, and it is called straight away.

The second advantage is that an array of almost any size is likely to sort more quickly.
Firstly, because van den Hoven’s Twinsort (GitHub - scandum/twinsort: An adaptive mergesort · GitHub) is tolerably fast (usually faster than Odin’s sort.quick_sort, although not as fast as C++ std::stable_sort).
Secondly, because techniques like shell sort and even radix sort can be very helpful to have on tap in particular cases without having to remember to ask for them.

The third advantage is that you keep getting a reasonable choice of sort algorithm as your arrays and structures grow. This reduces the amount of backend chug and frontend lag you may expect as your sorts work with larger and larger objects. More on this below.
All of the algorithms and methods used can be called individually - and they each of them have documentation helping you decide whether to choose them.

Arrays of structs

Sort arrays-of-structs on multiple potential criteria, and you really start wanting a fast, stable sort, one that doesn’t mess up the order of equal-value elements. Twinsort is both. Call “xsort.sort_stable_cmp()” and you get convenience similar to “sort.quick_sort_proc()”, plus more speed, plus stability.
Plus automatic adaptation to your use case. If you feed xsort.sort_stable_cmp() or xsort.sort_unstable_cmp() an array of 2 or a million elements, or elements of 1 byte or ten thousand, you can expect it to adapt.

Indexed sorting

This adaptation is made very much capable by indexed sorting, analogous to what the slice and sort packages offer, but made more accessible, more extensively documented, and even automatic. The details are covered in the guide and test suite, but some of the reasons you may want to call “gen_sorted_index_stable()” and “reorder_from_index()” frequently are demonstrated in xsort/tests/5., and include:

  • Saving your sorts.
  • Issuing multi-parameter sorts.
  • Getting faster sorts on arrays with large elements (>~50 bytes; see sort_stable_cmp()
  • for details).
  • Sorting Structures-of-Arrays.
  • Being able to avoid sorting a particularly heavyweight array at all, choosing to
  • instead search on or display it using the sort index.

Early Days yet

However, this library is very much a work in progress! It’s marked “1.0” because it contains what I consider to be the minimum viable suite of algorithms and methods to deliver real value to the Odin development community. It’s marked as a mere “release candidate” because it can only get to Version 1.0 if it gets enough testing and evaluation to make sure that it does in fact fulfill the motto

Be Safe. Be Right. Be Clear. Be Fast. In that order.

xsort hopes for your help to get there. Please check it out if you have a moment.

Respectfully yours,
Alexander Swift

1 Like

Very cool. Now this is something I can geek out on. Thanks for sharing.

I like that the library and test suites are separate so that when using the actual library as a shared import, main is not included in a project.

I’d like to make a suggestion on the package structure.

If I do a direct clone from git, the actual library is in xsort/xsort, which means I’d have to import with the following, which feels redundant:

import "shared:xsort/xsort"

Maybe it is better if the library is in root of xsort, and all the tests are in the sub-directory “test” or maybe “tests”?

I like it. Directory reorganized.