1// Package tablesort provides functionality to render a Markdown table with sortable columns.2// It allows users to click on column headers to sort the table in ascending or descending sort direction.3// The sorting state is managed via URL query parameters.4// It displays an error if the table is malformed (e.g. rows with missing cells).5// Multiple tablesort can be rendered on the same page by using a paramPrefix for each Render (See the Render function).6package tablesort78import (9 "sort"10)1112// rowSorter implements sort.Interface for sorting rows by a specific column.13type rowSorter struct {14 rows [][]string15 colIndex int16 ascending bool17}1819func (rs rowSorter) Len() int {20 return len(rs.rows)21}2223func (rs rowSorter) Less(i, j int) bool {24 iCell := rs.rows[i][rs.colIndex]25 jCell := rs.rows[j][rs.colIndex]26 if rs.ascending {27 return iCell < jCell28 }29 return iCell > jCell30}3132func (rs rowSorter) Swap(i, j int) {33 rs.rows[i], rs.rows[j] = rs.rows[j], rs.rows[i]34}3536// SortRows sorts the rows slice by a given column index and direction37func SortRows(rows [][]string, colIndex int, ascending bool) {38 sort.Sort(rowSorter{rows, colIndex, ascending})39}40Signatures reconstructed verbatim from vm/qfuncs — interface params keep their inline definitions.