Why `sort()` need `T` to be `Ord`?

Decades of industrial experience and probably hundreds or thousands of CVEs disagree with you.

7 Likes

Yes, there is a bug in my Ord's implementation. But sort() works well and I didn't find this bug before.
So, I think sort() require PartialOrd would be better.

Look at what H2C03 said....
Also Michael-F-Bryan gives the technical reason behind it....

2 Likes

This one persuades me.
Yep, also some historical reasons. That makes sense.

https://stackoverflow.com/questions/70588237/why-sort-need-t-to-be-ord/70588789#70588789

That is very surprising. While technically fine because requirements say this is equivalent to using cmp when Ord is implemented, I think it should be nevertheless changed to use Ord::cmp.

Let's look at this from a mathematical p.o.v.

There are partial orders and weak orders (in form of a "strict weak order", or a "non-strict weak order", of which the latter is also called "total preorder", see linked Wikipedia article).

IMHO sorting a slice based on a partial order usually makes no sense because there could be two subsets which each can be sorted, but there is no relation between two elements when each of these elements is in a different of the two subsets.

For example:

  • USD 50.00, USD 43.22, EUR 1.00, EUR 2.00

We could define a partial order where equal currencies can be compared, while different currencies don't compare. Possible sorting results could be:

  • USD 43.22, USD 50.00, EUR 1.00, EUR 2.00
  • EUR 1.00, EUR 2.00, USD 43.22, USD 50.00

I would claim that it is semantically wrong to call this "sorting" the list. I would say you could "partially" sort it (which is something different than an "unstable" sort).

However, if we have a weak order, e.g. ordering rectangles based on their area, then we could easily sort these. But there is no trait for a weak order in Rust! :scream:

1 Like

I mean, you totally can do the sort though:

rects.sort_by_key(|rect| rect.area());
1 Like

OK I see what the reason for this is:
https://github.com/rust-lang/rust/pull/39538

It's faster when lt() can do less work than cmp when it's specialized.

I'm not sure why it's actually (very slightly) faster for built-in types, according to that ticket, since the compiler should be able to see the two ways to compare built-in types are equivalent.

Yes, but you can't do this with .sort (which uses Ord) unless you make two rectangles with the same area equal.

Thus there is no trait to represent the weak ordering. It's represented through the closure |rect| rect.area() instead.

P.S.: My screaming smiley was an exaggeration. I guess there isn't really a need to introduce a trait for weak orders, as these can always be achieved easily through a sorting key like you suggested. I just wanted to point out that PartialOrd shouldn't be confused with WeakOrd (which doesn't exist).

Could you expand on there is no trait for a weak order in Rust? If there are partial orders and those are PartialOrd, and there are weak orders but those are not Ord, then what is Ord?

IMO, Ord is definitely a trait for weak orders. It's just incorrectly documented (it should say "total preorder", yet it does say "total order" im the docs). Look at Eq which is correctly documented as a trait for equivalence relations, not for equality. The distinction between stable and unstable sorting algorithms only really makes sense for weak orders, too. (The German version of the Wikipedia article also does point that out.)

(The use of "total order" in the docs makes some sense if the equivalence by the Eq implementation is (mis-)interpreted as equality. After all, a total preorder is the same as an actual total order on the equivalence classes.)

3 Likes

Because that was before C++20. I'm not joking.

GCC got the appropriate optimization only very recently, I wouldn't be surprised that LLVM was also missing it at the time when bug was created.

According to the documentation, Ord is a total order. That also makes sense to me, because it will always compare an element to another element as either being smaller or greater, unless it is equal.

Isn't a weak order the same as a total order if we simply put all non-comparable items in the same equivalence class? Maybe these two are actually the same and it's just a semantic difference?

Now I'm confused :sweat_smile:

But I just noticed that a "total order" indeed depends on how equality is defined (or a used equivalence relation). And the Ord trait does require that equality (or a certain equivalence relation defined through Eq) does behave in a certain way.

Yes, Eq could be any equivalence relation.

Now the question is… Is the equality / non-equality operator here an arbitrary equivalence relation? I would say yes, because we're not operating with numbers or any particular objects here in the mathematical definition. If yes, then I'd say Ord can be seen as "total order".

My point is: Ord interacts with Eq, and thus I'd say it's correct to describe it as total order.

I would say:

  • Ord unambiguously sorts all elements (unless some elements are equal to one another according to Eq, in which case their sorting can vary).
  • WeakOrd (which doesn't exist) could treat some elements as "equal in regard to the sorting", independently on how Eq (== operator) is defined for that type.
  • PartialOrd can treat some elements as "could appear anywhere in the list". Floating point numbers have a partial order, because NaN could appear anywhere in a sorted list.

No, it's not an arbitrary equivalence relation.

Mathematical notion of equality is defined for all mathematical objects. On a fundamental level in first-order logic you already have a notion of equality. It asserts basic properties like the ability to always being able to substitute equal things with equal things in statements or expressions. This it related to the notion of equality by Leibniz which basically says that things are equal if and only if they have the same properties. Or if you take a set theoretic foundation, that comes with a more concrete notion of equality by means of sets having the same elements.

Docs actually use both terms in the first statement:

Trait for equality comparisons which are equivalence relations.

I think there are two levels of description here:

  • on the representation level, it's an equivalence relation
  • on a higher, semantic level, it's equality

Ah okay! I guess you're right then.

In that case, either Eq would need to be redefined as "equality" (instead of "equivalence relation") or Ord would need to be redefined as "weak order" (instead of "total order").

Do I understand it correctly?

For instance:

Two Vec with different capacities but same sequence of values have different bit representations underneath, but represent the same mathematical object.

== implements the equivalence relation on representations, but it also means equality on the mathematical objects that are represented.

To give everything a name (feel free to point out mistakes)

PartialEq - partial equivalence relation
Eq - equivalence relation
PartialOrd - partial preorder (hard to find on English Wikipedia) Edit: that's not quite right...
PartialOrd + Eq - preorder
Ord - total preorder

(I'm ignoring S: PartialEq<T> or S: PartialOrd<T> between different types. Those are hard/impossible to characterize.)

Thanks for sharing, I'll look into that later. I have a preliminary comment already:

If I understand it right, then a "total preorder" is a weak order, and as such it doesn't need any equivalence relation being defined (but Ord does). (Even though you could derive an equivalence relation from a weak order, of course.)

I also noted @tczajka's comments that == often means equality when used with mathematical objects.

I'll look into it later again. Thanks for all your feedback so far!