我正在查看 sort.Reverse 代码:
type reverse struct {
// This embedded Interface permits Reverse to use the methods of
// another Interface implementation.
Interface
}
// Less returns the opposite of the embedded implementation's Less method.
func (r reverse) Less(i, j int) bool {
return r.Interface.Less(j, i)
}
// Reverse returns the reverse order for data.
func Reverse(data Interface) Interface {
return &reverse{data}
}
据我所知,reverse 实现了 Interface(并覆盖了 Less)而 *reverse 没有实现接口(interface)。为什么 Reverse 返回 *reverse(不知何故是 Interface)?
最佳答案
对于您的评论:
As far as I understand reverse implements Interface (and overrides Less)
Golang 中没有方法覆盖。它不会覆盖 Less。它正在实现 Interface 的 Less,这是一个 interface 类型:
type Interface interface {
// Len is the number of elements in the collection.
Len() int
// Less reports whether the element with
// index i should sort before the element with index j.
Less(i, j int) bool
// Swap swaps the elements with indexes i and j.
Swap(i, j int)
}
对于提出的问题:
Why does Reverse return *reverse (which somehow is Interface)
因为要改变数据的顺序,所以它应该是反向结构的地址。这就是为什么它是 *reverse 的原因。现在您可以传递任何实现 Interface 接口(interface)的类型。为了实现 Interface,您应该实现 Interface 中定义的所有方法。
type IntSlice []int
func (p IntSlice) Len() int { return len(p) }
func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p IntSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
在上面的例子中你可以看到 IntSlice 实现了 Interface。因此,您可以将 Intslice 作为参数传递给 Reverse。
例如,您还可以使用 Float 值来实现:
package main
import (
"fmt"
"sort"
)
func main() {
//s := []int{5, 2, 6, 3, 1, 4} // unsorted
s1 := []float64{5.2, 2.6, .6, .03, 2.1 } // unsorted
sort.Sort(sort.Reverse(sort.Float64Slice(s1)))
fmt.Println(s1)
}
关于sorting - sort.Reverse 在 Go 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51725601/
两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option
我已经搜索过这个问题的答案,但没有成功,有一个类似的问题,但答案在这种情况下不起作用,它按数字项目排序。SimilarQuestion-Thatdidnotwork我正在尝试使用ruby的sort_by对一个项目进行降序排序和另一个升序排序。我只能找到一个。代码如下:#PrimarysortLastNameDescending,withtiesbrokenbysortingAreaofinterest.people=people.sort_by{|a|[a.last_name,a.area_interest]}任何指导肯定会有所帮助。示例数据:输入罗素,逻辑欧拉,图论伽罗瓦,抽象代
在Railcasts上,我注意到一个非常有趣的功能“转到符号”窗口。它像Command-T一样工作,但显示当前文件中可用的类和方法。如何在vim中获取它? 最佳答案 尝试:helptags有各种程序和脚本可以生成标记文件。此外,标记文件格式非常简单,因此很容易将sed(1)或类似的脚本组合在一起,无论您使用何种语言,它们都可以生成标记文件。轻松获取标记文件(除了下载生成器之外)的关键在于格式化样式而不是实际解析语法。 关于ruby-on-rails-Textmate'Gotosymbol
我需要按照作为搜索参数传入的准确顺序查找记录。例如,我有一个字符串:item_list="23,12,54,45"通过以下查询,我按“item_list”的asc顺序获取记录-“12,23,45,54”。Inventory.find(item_list.split(","))如何修改上述查询,使其以与“item_list”相同的顺序返回记录。谢谢。 最佳答案 试试这个,虽然它可能只适用于MySQL:Inventory.where("idIN(#{item_list})").order("find_in_set(id,'#{item_
我想按数据库表列“plays”对其进行排序/排序(按我想要的方式降序或升序)我完全糊涂了。刚刚找到了select而不是collection_select的解决方案?我的一些代码不知道如何排序/排序数据库表中还有一些列,如“plays”、“goals”... 最佳答案 只需将实际排序的集合传递给collection_select助手:collection_select(:post,:author_id,Author.order('created_atDESC'),:id,:name_with_initial,:prompt=>true
自97年以来我一直在使用vi/vim进行各种快速编辑和管理任务,但最近才考虑使用它来替换Netbeans作为我选择的ruby编辑器。我发现一件事在Netbeans和Eclipse中非常有用的是Ctrl+Click“转到定义”功能,您可以在其中按住Ctrl键并单击一个类或方法,然后它将带您了解定义。现在,我玩过丰富的ctags和rails.vim,而且很接近,但没有雪茄。这就是我想要的:默认情况下在Netbeans和Eclipse中,您可以在本地rails中按住ctrl并单击本地方法或类项目,但你也可以ctrl+click定义在gems或用Ruby编写的系统库。以Netbeans为例
ActiveAdmin似乎还不支持多列排序(即将多个值传递给config.sortable选项)。我看到一个老猴子补丁here但它似乎不适用于我的版本(来自Github的1.0.0.pre)。有没有办法在最新的ActiveAdmin版本上获得多个可排序的列? 最佳答案 这也是一个猴子补丁:在config/initializers或lib文件夹中创建一个新文件:multiple_columns_sorting.rbmoduleActiveAdminclassResourceController重启服务器。现在您可以使用由"_and_"
Dir.glob("*.txt"){|f|pf}打印文件名。Dir.glob("*.txt").sort{|f|pf}因ArgumentError失败。Dir.glob("*.txt").sort.each{|f|pf}按字母顺序打印文件名。为什么第二个失败了?更好的是,为什么第一个工作,有或没有.each?Dir.glob和Dir.glob.sort都是数组。Dir.glob.methods==Dir.glob.sort.methods。(受AlphabetizeresultsofDir.glob启发。不是Dir.globwithsortissue的副本,因为“第三个”已经回答了那个
我正在使用运行一个简单的查找全部并使用willpaginate分页,但我也希望由用户对查询进行排序。想到的第一个解决方案就是使用params[:sort]http://localhost:3000/posts/?sort=created_at+DESC@posts=Post.paginate:page=>params[:page],:order=>params[:sort]但他的方法的问题是查询默认为按ID排序,我希望它是created_at。这是一种安全的排序方法吗?有没有办法默认使用created_at? 最佳答案 我会使用命名
这不是一个技巧问题:[1,2,3].sort_by{|x,y|xy}=>[1,2,3][1,2,3].sort_by{|x,y|yx}=>[1,2,3]这是怎么回事?我原以为数组会彼此相反(因为它们具有排序和相同的参数)。 最佳答案 #sort_by应该只采用一个block参数,数组中的一项,并根据block的结果进行排序。当向它传递两个block参数时,第二个设置为nil因此所有block结果都像1nil这是nil所以数组的顺序不变。[1,3,2].sort_by{|x|x}#sortsusingxy=>[1,2,3][1,3,2