[bug #68604] sort() is not stable for sparse arrays containing multiple NaNs

"Dmitri A. Sergatskov" <[email protected]>
Newsgroups gmane.comp.gnu.octave.bugs
Message-ID <[email protected]>
Please use the bug tracker to post updates to a bug report.  The mailing list is intended as a read-only notification stream.  Info posted to this mailing list address won't appear in the tracker database where it is most useful.

URL:
  <https://savannah.gnu.org/bugs/?68604>

                 Summary: sort() is not stable for sparse arrays containing
multiple NaNs
                   Group: GNU Octave
               Submitter: dasergatskov
               Submitted: Sat 08 Aug 2026 10:43:25 PM UTC
                Category: Octave Function
                Severity: 3 - Normal
                Priority: 5 - Normal
              Item Group: Incorrect Result
                  Status: None
             Assigned to: None
         Originator Name:
        Originator Email:
             Open/Closed: Open
         Discussion Lock: Unlocked
                 Release: dev
        Operating System: Any
           Fixed Release: None
         Planned Release: None


    _______________________________________________________

Follow-up Comments:


-------------------------------------------------------
Date: Sat 08 Aug 2026 10:43:25 PM UTC By: Dmitri A. Sergatskov <dasergatskov>
Currently `sort()`  does not preserve the input order of multiple NaN entries
when sorting sparse arrays. Dense arrays preserve their order, but sparse
arrays reverse them. 
For example:

octave:1>  x = [NaN, NaN, 1]
x =

   NaN   NaN     1

octave:2> [y, d_idx] = sort (x)
y =

     1   NaN   NaN

d_idx =

   3   1   2

octave:3> [s_y, s_idx] = sort (sparse(x))
s_y =

Compressed Column Sparse (rows = 1, cols = 3, nnz = 3 [100%])

  (1, 1) -> 1
  (1, 2) -> NaN
  (1, 3) -> NaN

s_idx =

   3   2   1


This becomes observable in the complex case:

octave:9> x = complex ([NaN, NaN, 1], [1, 2, 0])
x =

   NaN +   1i   NaN +   2i     1 +   0i

octave:10> y = sort(x)
y =

     1 +   0i   NaN +   1i   NaN +   2i

octave:11> s_y =full(sort(sparse(x)))
s_y =

     1 +   0i   NaN +   2i   NaN +   1i



The problem is with Sparse-c/d code, not the sort itself. I should have a
patch soon.

Dmitri.
-- 








    _______________________________________________________

Reply to this item at:

  <https://savannah.gnu.org/bugs/?68604>

_______________________________________________
Message sent via Savannah
https://savannah.gnu.org/
signature.asc (application/pgp-signature, 228 B)
-----BEGIN PGP SIGNATURE-----

iHUEABYIAB0WIQQk97aszIMMAvLLwm6qLAuaBUf3TgUCanexEQAKCRCqLAuaBUf3
Tml5AP4gVsO27rhgP7IWVr8ws2t95ELhG45mnFxT86PxDDpXfwD/ZMoqIFjrvaqD
S9wfxkEjPBugef4/rpgdgmU8/Ou4wwk=
=LGP6
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.