Reputation: 10819
I have a sequence generated by list comprehension as follows:
var a_bigram_list = lc[a[i..i+2] | (i <- 0..<len(a)), string]
Now, I would like to sort it but sort(a_bigram_list)
will result in the following compilation error
Error: type mismatch: got (seq[string])
but expected one of:
proc sort[A, B](t: OrderedTableRef[A, B]; cmp: proc (x, y: (A, B)): int)
proc sort[A, B](t: var OrderedTable[A, B]; cmp: proc (x, y: (A, B)): int)
proc sort[A](t: CountTableRef[A])
proc sort[A](t: var CountTable[A])
proc sort[T](a: var openArray[T]; cmp: proc (x, y: T): int; order = SortOrder.Ascending)
Is there any way of sorting a sequence? Or do I need to convert it to array? If so, can is there a way to obtain an array from lc?
Upvotes: 10
Views: 3673
Reputation: 410
As of October 2018, your intended code sort(a_bigram_list)
will now work just fine, since sort[T](...)
now uses system.cmp[T]
as the implied sort function if you only provide one argument.
Upvotes: 3
Reputation: 457
sort
works with sequences (openArray
is a generic parameter type that accepts both array
s and seq
s), but it expects a comparison proc as a second parameter.
You can provide it a default cmp from system module:
sort(a_bigram_list, system.cmp)
Upvotes: 11