diff --git a/pint/delegates/formatter/html.py b/pint/delegates/formatter/html.py
index cc7d9ad07..98329bcca 100644
--- a/pint/delegates/formatter/html.py
+++ b/pint/delegates/formatter/html.py
@@ -78,6 +78,12 @@ def format_unit(
self, unit: PlainUnit, uspec: str = "", **babel_kwds: Unpack[BabelKwds]
) -> str:
units = format_compound_unit(unit, uspec, **babel_kwds)
+ if unit._REGISTRY.formatter.default_sort_func is not None:
+ sort_func = lambda x: unit._REGISTRY.formatter.default_sort_func(
+ x, unit._REGISTRY
+ )
+ else:
+ sort_func = None
return formatter(
units,
@@ -87,9 +93,7 @@ def format_unit(
division_fmt=r"{}/{}",
power_fmt=r"{}{}",
parentheses_fmt=r"({})",
- sort_func=lambda x: unit._REGISTRY.formatter.default_sort_func(
- x, unit._REGISTRY
- ),
+ sort_func=sort_func,
)
def format_quantity(
diff --git a/pint/delegates/formatter/plain.py b/pint/delegates/formatter/plain.py
index 37150f9d2..850e2eab5 100644
--- a/pint/delegates/formatter/plain.py
+++ b/pint/delegates/formatter/plain.py
@@ -259,6 +259,12 @@ def format_unit(
self, unit: PlainUnit, uspec: str = "", **babel_kwds: Unpack[BabelKwds]
) -> str:
units = format_compound_unit(unit, uspec, **babel_kwds)
+ if unit._REGISTRY.formatter.default_sort_func is not None:
+ sort_func = lambda x: unit._REGISTRY.formatter.default_sort_func(
+ x, unit._REGISTRY
+ )
+ else:
+ sort_func = None
return formatter(
units,
@@ -269,9 +275,7 @@ def format_unit(
power_fmt="{}{}",
parentheses_fmt="({})",
exp_call=pretty_fmt_exponent,
- sort_func=lambda x: unit._REGISTRY.formatter.default_sort_func(
- x, unit._REGISTRY
- ),
+ sort_func=sort_func,
)
def format_quantity(