Sort based on original order, not last sort

Tags: #<Tag:0x00007f0b09c32ab0> #<Tag:0x00007f0b09c32830>

After upgrading from HT 7 to HT 12 I noticed a slight change in sorting behavior. (Because we skipped over 8, 9, 10, 11, I have no idea what version this changed, it could be ancient history!)

Before, when I sorted on a column, it would ignore any previous sorts and basically sort the data based off of the original data order.

Now, when I sort on a column it preserves the prior sort when resolving ties.

Example: https://jsfiddle.net/p4otw0hr/

Sort on Name ascending, then on Color ascending, and you get this: image

Sort on Name descending, then on Color ascending, and you get this: image

The end result is that a state of “Sorted by Color ascending” has two different results. I am guessing that this is actually by design (I noticed that Google Sheets does it the same way, except Sheets has no sort indicator so you could argue it’s a different kind of feature), but is there any option to make sorting always based off of the original order of data?

I found what appears to be a pretty simple workaround:

  beforeColumnSort(prev, next) {
    if (prev.length && next.length) {
      hot.getPlugin("columnSorting").clearSort();
    }
  }

Works for multiColumnSorting as well.

It would be nice if this was a new config option!

Hi @abeall

That’s correct. we introduced many changes since version 8 to unify plugins with other spreadsheets on the market. So, yes, it was done by the design and most possibly, it won’t be changed again to the previous state.

The workaround you proposed looks good and I would recommend to use it if you want/need to achieve the previous behaviour.

1 Like

Thanks, that makes sense.

Do you think that a config option like alwaysSortBasedOnOriginalOrder or something would be useful? Would you consider a PR that adds that? This behavior actually came up as a bug report for us, I see how the intended design makes sense, but I think the confusion is that since there is an original order the table maintains (unlike Sheets) and an indicator that implies an effect against the original data, the sort action seemed inconsistent on columns with a lot of ties.

Hi @abeall

I think we already have something similar reported here: https://github.com/handsontable/handsontable/issues/10137

This issue has status to be discussed. I suggest you to add your ideas to it so when we start investigating it we will have more information, and with your workaround, a possible solution :slight_smile:

1 Like

Will do, thanks!