go-pretty icon indicating copy to clipboard operation
go-pretty copied to clipboard

Transformer affects sorting

Open muesli opened this issue 4 years ago • 3 comments

Describe the bug I expected SortBy to operate on the raw table data. However as soon as I set a transformer on the column I sort by, the sorting breaks.

Software (please complete the following information):

  • OS: Linux
  • GoLang Version 1.15.2

muesli avatar Sep 23 '20 18:09 muesli

I'll look into this, but it'll be awesome if you could share a code snippet or simple example reproducing the bug.

jedib0t avatar Sep 23 '20 20:09 jedib0t

Hey I figured what is happening. The sorting is done after converting every column to string and that includes the calls to the column transformers.

I'll see if I can provide an option to sort before conversion to string/transformation, but I can't commit to an ETA.

I cannot switch it to be the default behavior as it will not be backwards compatible to anyone expecting the sorting to be done after transformation.

jedib0t avatar Sep 26 '20 05:09 jedib0t

I see, thanks for the feedback! Maybe we could add this as a setting to the SortBy struct?

muesli avatar Sep 27 '20 20:09 muesli

Hey! I finally got around to working on a solve for this when I realized that the "transformers" are invoked even before I convert the column contents into string. So there is no realistic way for me to compare and sort the contents before transformation since I cannot do < check on two interface{} objects.

Workaround I'd suggest: have an additional column with the original values and use that for sorting instead of the "transformed" column; but hide the column using ColumnConfig.Hidden.

Edit: OMG I just realized I replied after 2+ years. I apologize for the obscene delay.

jedib0t avatar Nov 07 '22 04:11 jedib0t

No worries :smiley: I ended up using that workaround in duf fwiw. It's not ideal, but I understand why it's problematic.

muesli avatar Nov 11 '22 01:11 muesli