mathjs icon indicating copy to clipboard operation
mathjs copied to clipboard

setDistinct changes order

Open DavidMulder0 opened this issue 1 month ago • 2 comments

Nothing in the documentation for setDistinct implies anything about sorting the array/matrix, yet:

math.setDistinct([3, 1, 2]); // [1, 2, 3]
math.setDistinct(['h', 'e', 'l', 'l', 'o']); // ['e', 'h', 'l', 'o']

Expected

math.setDistinct([3, 1, 2]); // [3, 1, 2]
math.setDistinct(['h', 'e', 'l', 'l', 'o']); // ['h', 'e', 'l', 'o']

Docs: setDistinct

Code: .sort(compareNatural)

DavidMulder0 avatar Nov 24 '25 07:11 DavidMulder0

Thanks. I've added a sentence to the description to explain that the results will be sorted, see 4bbe862.

However, I would prefer the result not to be sorted. It shouldn't be hard to change the code to leave the original order as-is. It will be a breaking change so I'll schedule it for the first next major version.

Who can implement this change? Help would be welcome.

josdejong avatar Nov 26 '25 10:11 josdejong

Hi Josdejong, fixed and created a pull request - https://github.com/josdejong/mathjs/pull/3603. Also added new test cases. Can you please look into it.

dhimansachit avatar Nov 26 '25 18:11 dhimansachit