FAQ
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




--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Volker Dobler at Sep 8, 2015 at 7:08 am

    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.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Giulio Iotti at Sep 8, 2015 at 7:15 am

    On Tuesday, September 8, 2015 at 2:17:08 AM UTC+2, Martin Johnson wrote:
    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?
    Does this example clarify a bit? https://play.golang.org/p/2uvmO_z8QA

    --
    Giulio Iotti

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Martin Johnson at Sep 8, 2015 at 7:52 pm
    Hi again,

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

    Kind regards

    Martin


    On Tuesday, September 8, 2015 at 1:17:08 AM UTC+1, Martin Johnson wrote:

    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



    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Gyu-Ho Lee at Sep 9, 2015 at 1:43 am
    I have code for this kind of problem, given that the slice is of string
    slice.
    https://github.com/gyuho/learn/tree/master/doc/go_sort_algorithm#sort-table

    Thanks,
    On Tuesday, September 8, 2015 at 12:52:24 PM UTC-7, Martin Johnson wrote:

    Hi again,

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

    Kind regards

    Martin


    On Tuesday, September 8, 2015 at 1:17:08 AM UTC+1, Martin Johnson wrote:

    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



    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Martin Johnson at Sep 11, 2015 at 7:27 am
    Gyu-Ho Lee,

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

    Kind regards

    Martin

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 8, '15 at 12:17a
activeSep 11, '15 at 7:27a
posts6
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase