jedib0t / go-pretty

Table-writer and more in golang!
MIT License
2.82k stars 113 forks source link

Table.SortBy can't handle empty cells #305

Closed Skeeve closed 6 months ago

Skeeve commented 6 months ago

Describe the bug

The last column of your test table has no values for two rows. Sorting that column fails.

To Reproduce

Put this into table_test.go:

func TestTable_MoreSort(t *testing.T) {
    table := Table{}
    table.AppendRows(testRows)
    table.AppendHeader(testHeader)
    table.SetStyle(styleTest)
    expectedOut := `(-----^------------^-----------^--------^-----------------------------)
[<#  >|<FIRST NAME>|<LAST NAME>|<SALARY>|<                           >]
{-----+------------+-----------+--------+-----------------------------}
[<20 >|<Jon       >|<Snow     >|<  2000>|<You know nothing, Jon Snow!>]
[<300>|<Tyrion    >|<Lannister>|<  5000>|<                           >]
[<1  >|<Arya      >|<Stark    >|<  3000>|<                           >]
\-----v------------v-----------v--------v-----------------------------/`
    table.SortBy([]SortBy{{Number: 5, Mode: Dsc}, {Name: "Last Name", Mode: Asc}})
    assert.Equal(t, expectedOut, table.Render())
}

Expected behavior

Expactation is that empty cells are sorted according to their string, representation.

The expected output is given in the code.

Screenshots

Actual output:

(-----^------------^-----------^--------^-----------------------------)
[<  #>|<FIRST NAME>|<LAST NAME>|<SALARY>|<                           >]
{-----+------------+-----------+--------+-----------------------------}
[<300>|<Tyrion    >|<Lannister>|<  5000>|<                           >]
[< 20>|<Jon       >|<Snow     >|<  2000>|<You know nothing, Jon Snow!>]
[<  1>|<Arya      >|<Stark    >|<  3000>|<                           >]
\-----v------------v-----------v--------v-----------------------------/

As you see: Column 5 isn't properly sorted.

Software (please complete the following information):

jedib0t commented 6 months ago

Release with fix: https://github.com/jedib0t/go-pretty/releases/tag/v6.5.5