Repository: cantino/mcfly (language: rust)

Issue:
Search panics with `user-provided comparison function does not correctly implement a total order`
Time to time the search functionality (both triggered by `ctrl + r` or `mcfly search`) crashes with:
```
thread 'main' panicked at core/src/slice/sort/shared/smallsort.rs:860:5: user-provided comparison function does not correctly implement a total order
```

The panic comes from this part of the code: https://github.com/cantino/mcfly/blob/master/src/history/history.rs#L408-L449. 

This is the stacktrace:
```
 $ thread 'main' panicked at core/src/slice/sort/shared/smallsort.rs:860:5:
user-provided comparison function does not correctly implement a total order
stack backtrace:
   0:        0x10019e3f0 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::habbf9c4f641febb1
   1:        0x10007ccac - core::fmt::write::ha36a8060c13608ea
   2:        0x100177194 - std::io::Write::write_fmt::h431832c8ebcc85c9
   3:        0x1001a1ba8 - std::panicking::default_hook::h4ebc6eb4ae179807
   4:        0x1001a2ac0 - std::panicking::rust_panic_with_hook::h6a84efe4dcab239c
   5:        0x1001a1e18 - std::panicking::begin_panic_handler::{{closure}}::h5eef292190467fef
   6:        0x1001a1db4 - std::sys::backtrace::__rust_end_short_backtrace::hd7e7925203f20af9
   7:        0x1001a1da8 - _rust_begin_unwind
   8:        0x100327284 - core::panicking::panic_fmt::h410d3f147658259b
   9:        0x100327714 - core::slice::sort::shared::smallsort::panic_on_ord_violation::h839471df44ebe912
  10:        0x1000c91d4 - core::slice::sort::stable::quicksort::quicksort::h0facaa6d199e1507
  11:        0x1000d0c34 - core::slice::sort::stable::drift::sort::h9266b210b2e53b2e
  12:        0x1000d0ae4 - core::slice::sort::stable::driftsort_main::hbef73e36ca0d9fe9
  13:        0x1000a1b90 - mcfly::history::history::History::find_matches::hf20bc79d3d9c53d5
  14:        0x1000c3b14 - mcfly::interface::Interface::display::h3d4bd061a91b7141
  15:        0x100003e98 - mcfly::main::h10d8a17722a95eef
  16:        0x1000f0cac - std::sys::backtrace::__rust_begin_short_backtrace::h88a42488dd053506
  17:        0x1000093b4 - _main
Process 86147 exited with status = 101 (0x00000065)
```

And I was also able to get values that cause the crash, see this Rust playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c6dae2dc0d0e3d2788cd870c19c42ee7

I checked NaN values in the calculations and couldn't find any NaN value, and also verified that `PartialOrd::partial_cmp` at the end never falls to `unwrap_or` fallback. I suspect this is due to sort implementations in stdlib [detecting incorrect `Ord` implementations](https://blog.rust-lang.org/2024/09/05/Rust-1.81.0.html#new-sort-implementations) and panicking in that case starting with Rust 1.81.


Discussion / hints:
Thanks @unexge! I've attempted a fix in https://github.com/cantino/mcfly/pull/451

Fix patch:
diff --git a/src/history/history.rs b/src/history/history.rs
index 5862e31..cee243e 100644
--- a/src/history/history.rs
+++ b/src/history/history.rs
@@ -408,7 +408,7 @@ impl History {
         if fuzzy > 0 && result_sort != &ResultSort::LastRun {
             names = names
                 .into_iter()
-                .sorted_by(|a, b| {
+                .sorted_unstable_by(|a, b| {
                     // Fuzzy matches impose new ordering criteria on top of the
                     // natural rank-based sorting: at the most basic level,
                     // shorter and earlier matches are more likely to be


Test patch: