duano3557 2018-01-15 05:35
浏览 38


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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 如何通过交互分析得出某高危患者对放疗获益更多
  • ¥15 相关性分析中,p<0.05, r=0.29,怎么评价相关性呢
  • ¥15 docker部署Mongodb后输入命令报错?
  • ¥15 将下列流程图转变成python程序代码
  • ¥15 我需要全国每个城市的最新小区名字等数据。
  • ¥15 开发一个小区生态的小程序
  • ¥15 如何解决Excel中dependent dropdown list 的问题
  • ¥15 MddBootstrapInitialize2失败
  • ¥15 LCD Flicker
  • ¥15 esp32在micropython环境下使用ssl/tls连接mqtt服务器出现以下报错Connected on发生意外错误: 5无法连接到 MQTT 代理,如何解决?