2018-01-15 05:35
浏览 27


I have table in golang. I need to sort it based on a particular column. Can someone help me to proceed?

table := ui.Table([]string{"SI No","Name","ID","status","submitted-at"})
strs := dat["resources"].([]interface{})
var ln int = 20
for i < ln {
    table.Add(strconv.Itoa(i+1), metadataName , metadataID, metadataStatus, metadataSubmittedAt)
    i = i + 1
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douqiandai4327 2018-01-16 06:43

    The sort package on the standard library includes an interface that if you implement it for any type then it can be sorted using the sort.Sort() function.

    Even if the ui.Table object is from a package or library you can't modify, you can always declare a new type using ui.Table as it's base and implement the sort.Interface yourself, something like:

    type myTable ui.Table
    func (m myTable) Len() int           { return len(m) }
    func (m myTable) Swap(i, j int)      { m[i], m[j] = m[j], m[i] }
    func (m myTable) Less(i, j int) bool { return m[i].col() < m[j].col() }

    Or you know some implementation that makes sense for the Table type. Here I'm assuming that your rows have a function col() that returns the value of the column you are using to sort for that row and that Table is a slice of rows which is probably not the case.

    I wish I could help more but it's not possible to help you with any implementation details if you don't give any details about ui.Table.

    点赞 打赏 评论

相关推荐 更多相似问题