Sorting 2d slice [][]string (slice of slices)

2,405 views
Skip to first unread message

Martin Johnson

unread,
Sep 7, 2015, 8:17:08 PM9/7/15
to golang-nuts
Hi, 

I'm connecting to an online API at http://seranking.com/api.html to obtain data for a client report
that ultimately ends up in Excel. 

The example nested JSON response is:

[ {"id":"1","keywords":[{"id":"1","positions":[{"date":"2015-09-03", "change":"1","pos":"1"},...]]}, .... ]

Once the JSON is successfully unmarshaled I'm then writing the data to a CSV file using "encoding/csv". 

The CSV output of my slice of slices (csvSlice[][]) looks like this:

keyword phrase one,http://www.example.co.uk/landing/example1,90
keyword phrase two
,http://www.example.co.uk/landing/example2,6
keyword phrase three
,http://www.example.co.uk/landing/example3,9
keyword phrase four
,http://www.example.co.uk/landing/example4,78
...

I'd like to sort the slices prior to creating the csv file so the above would output like the below (sorted by last string, which is the position in the Google)

keyword phrase two,http://www.example.co.uk/landing/example2,6
keyword phrase three
,http://www.example.co.uk/landing/example3,9
keyword phrase four
,http://www.example.co.uk/landing/example4,78
keyword phrase one,http://www.example.co.uk/landing/example1,90
...

Currently I'm only able to sort the slice in the slice, for example I can do this:

6, http://www.example.co.uk/landing/example2, keyword phrase two
...

All the basic examples I can find relate to sorting a slice and not a slice of slices. 

people := []Person{
{"Bob", 31},
{"John", 42},
{"Michael", 17},
{"Jenny", 26},
}

Can anyone point me in the right direction? 

Thank you

Martin




Volker Dobler

unread,
Sep 8, 2015, 3:08:47 AM9/8/15
to golang-nuts


Am Dienstag, 8. September 2015 02:17:08 UTC+2 schrieb Martin Johnson:

All the basic examples I can find relate to sorting a slice and not a slice of slices.

These examples actually _do_ show how to sort a slice of slices:
It does not matter what the slice you want to sort consists of: The slice
you want to sort can be made of ints, structs, slices, maps or even 
slices of structs of ... You always sort a slice of 'records'.
All need to understand to adopt e.g. the example given in the sort package
is, that the Less method compares not the whole element of the slice (the
whole record) to sort, but just a certain sub-key, a certain aspect of the
record (here Age). Your records are slices and the you want to sort your
records on the third value of your records.
So your Less method will look something like.
   Less(i, j, int) bool { return a[i][2] < a[j][2] } 

V.

Giulio Iotti

unread,
Sep 8, 2015, 3:15:52 AM9/8/15
to golang-nuts
Does this example clarify a bit? https://play.golang.org/p/2uvmO_z8QA

-- 
Giulio Iotti 

Martin Johnson

unread,
Sep 8, 2015, 3:52:24 PM9/8/15
to golang-nuts
Hi again, 

Thank you both for the explanations and code examples! I have now successfully sorted the 
slices of slices. 

Kind regards

Martin

Gyu-Ho Lee

unread,
Sep 8, 2015, 9:43:49 PM9/8/15
to golang-nuts
I have code for this kind of problem, given that the slice is of string slice.

Thanks,

Martin Johnson

unread,
Sep 11, 2015, 3:27:24 AM9/11/15
to golang-nuts
Gyu-Ho Lee, 

Thank you for the additional code! Golang-nuts is turning out to be an incredible resource. 

Kind regards

Martin    
Reply all
Reply to author
Forward
0 new messages