Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort a sequence in Nim?

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?

like image 534
alec_djinn Avatar asked Mar 01 '17 11:03

alec_djinn


1 Answers

sort works with sequences (openArray is a generic parameter type that accepts both arrays and seqs), 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)
like image 93
Andrew Penkrat Avatar answered Oct 08 '22 22:10

Andrew Penkrat