Skip to content

Commit

Permalink
table: Sort should now work on missing/empty cells
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t committed Mar 14, 2024
1 parent 92ad2cc commit 45c29ac
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
45 changes: 26 additions & 19 deletions table/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,34 +93,41 @@ func (rs rowsSorter) Swap(i, j int) {

func (rs rowsSorter) Less(i, j int) bool {
realI, realJ := rs.sortedIndices[i], rs.sortedIndices[j]
for _, col := range rs.sortBy {
rowI, rowJ, colIdx := rs.rows[realI], rs.rows[realJ], col.Number-1
if colIdx < len(rowI) && colIdx < len(rowJ) {
shouldContinue, returnValue := rs.lessColumns(rowI, rowJ, colIdx, col)
if !shouldContinue {
return returnValue
}
for _, sortBy := range rs.sortBy {
rowI, rowJ, colIdx := rs.rows[realI], rs.rows[realJ], sortBy.Number-1
// extract the values/cells from the rows for comparison
iVal, jVal := "", ""
if colIdx < len(rowI) {
iVal = rowI[colIdx]
}
if colIdx < len(rowJ) {
jVal = rowJ[colIdx]
}
// compare and choose whether to continue
shouldContinue, returnValue := rs.lessColumns(iVal, jVal, sortBy)
if !shouldContinue {
return returnValue
}
}
return false
}

func (rs rowsSorter) lessColumns(rowI rowStr, rowJ rowStr, colIdx int, col SortBy) (bool, bool) {
if rowI[colIdx] == rowJ[colIdx] {
func (rs rowsSorter) lessColumns(iVal string, jVal string, sortBy SortBy) (bool, bool) {
if iVal == jVal {
return true, false
} else if col.Mode == Asc {
return false, rowI[colIdx] < rowJ[colIdx]
} else if col.Mode == Dsc {
return false, rowI[colIdx] > rowJ[colIdx]
} else if sortBy.Mode == Asc {
return false, iVal < jVal
} else if sortBy.Mode == Dsc {
return false, iVal > jVal
}

iVal, iErr := strconv.ParseFloat(rowI[colIdx], 64)
jVal, jErr := strconv.ParseFloat(rowJ[colIdx], 64)
iNumVal, iErr := strconv.ParseFloat(iVal, 64)
jNumVal, jErr := strconv.ParseFloat(jVal, 64)
if iErr == nil && jErr == nil {
if col.Mode == AscNumeric {
return false, iVal < jVal
} else if col.Mode == DscNumeric {
return false, jVal < iVal
if sortBy.Mode == AscNumeric {
return false, iNumVal < jNumVal
} else if sortBy.Mode == DscNumeric {
return false, jNumVal < iNumVal
}
}
return true, false
Expand Down
16 changes: 16 additions & 0 deletions table/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ func TestTable_sortRows_WithoutName(t *testing.T) {
assert.Equal(t, []int{0, 1, 2, 3}, table.getSortedRowIndices())
}

func TestTable_sortRows_MissingCells(t *testing.T) {
table := Table{}
table.AppendRows([]Row{
{1, "Arya", "Stark", 3000, 9},
{11, "Sansa", "Stark", 3000},
{20, "Jon", "Snow", 2000, "You know nothing, Jon Snow!"},
{300, "Tyrion", "Lannister", 5000, 7},
})
table.SetStyle(StyleDefault)
table.initForRenderRows()

// sort by "First Name"
table.SortBy([]SortBy{{Number: 5, Mode: Asc}})
assert.Equal(t, []int{1, 3, 0, 2}, table.getSortedRowIndices())
}

func TestTable_sortRows_InvalidMode(t *testing.T) {
table := Table{}
table.AppendRows([]Row{
Expand Down

0 comments on commit 45c29ac

Please sign in to comment.