FAQ
*I am having these structs:*

*
*

*type* Record *struct* {

...

Distance *string*

* ...*

Results *string*
}

*type* Response *struct* {

...

Version *string*

Records []Record

}


How to sort Response.Records array in ascending order based on
Record.Distance?



--
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Kamil Kisiel at Feb 13, 2013 at 6:52 am
    I hate to give the "read the documentation" kind of answers, but really,
    read the documentation :)

    This is almost exactly analogous to example given in the sort
    package: http://golang.org/pkg/sort/#example_Interface

    Please have a read and try to understand it, if you're still having
    problems then ask a more specific question.
    On Tuesday, February 12, 2013 10:20:46 PM UTC-8, Constantine Vasil wrote:

    *I am having these structs:*

    *
    *

    *type* Record *struct* {

    ...

    Distance *string*

    * ...*

    Results *string*
    }

    *type* Response *struct* {

    ...

    Version *string*

    Records []Record

    }


    How to sort Response.Records array in ascending order based on
    Record.Distance?


    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Patrick Mylund Nielsen at Feb 13, 2013 at 4:33 pm
    Maybe, but I have to sympathize. It's harder than it should be, and not
    very intuitive, to e.g. sort by a field or index.

    On Wed, Feb 13, 2013 at 7:52 AM, Kamil Kisiel wrote:

    I hate to give the "read the documentation" kind of answers, but really,
    read the documentation :)

    This is almost exactly analogous to example given in the sort package:
    http://golang.org/pkg/sort/#example_Interface

    Please have a read and try to understand it, if you're still having
    problems then ask a more specific question.

    On Tuesday, February 12, 2013 10:20:46 PM UTC-8, Constantine Vasil wrote:

    *I am having these structs:*

    *
    *

    *type* Record *struct* {

    ...

    Distance *string*

    * ...*

    Results *string*
    }

    *type* Response *struct* {

    ...

    Version *string*

    Records []Record

    }


    How to sort Response.Records array in ascending order based on
    Record.Distance?



    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Daniel Bryan at Feb 13, 2013 at 8:55 pm
    Implementing sort.Interface is fine.. I just wish I could pass the funcs
    for Less(), Swap() and Len() into the sort function in a pinch, instead of
    having to go open the file with my types and implement sort semantics for
    the type that really only apply in one spot.

    On Thursday, February 14, 2013 3:33:47 AM UTC+11, Patrick Mylund Nielsen
    wrote:
    Maybe, but I have to sympathize. It's harder than it should be, and not
    very intuitive, to e.g. sort by a field or index.


    On Wed, Feb 13, 2013 at 7:52 AM, Kamil Kisiel <[email protected]<javascript:>
    wrote:
    I hate to give the "read the documentation" kind of answers, but really,
    read the documentation :)

    This is almost exactly analogous to example given in the sort package:
    http://golang.org/pkg/sort/#example_Interface

    Please have a read and try to understand it, if you're still having
    problems then ask a more specific question.

    On Tuesday, February 12, 2013 10:20:46 PM UTC-8, Constantine Vasil wrote:

    *I am having these structs:*

    *
    *

    *type* Record *struct* {

    ...

    Distance *string*

    * ...*

    Results *string*
    }

    *type* Response *struct* {

    ...

    Version *string*

    Records []Record

    }


    How to sort Response.Records array in ascending order based on
    Record.Distance?



    --
    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 [email protected] <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Kyle Lemons at Feb 13, 2013 at 9:17 pm

    On Wed, Feb 13, 2013 at 12:55 PM, Daniel Bryan wrote:

    Implementing sort.Interface is fine.. I just wish I could pass the funcs
    for Less(), Swap() and Len() into the sort function in a pinch, instead of
    having to go open the file with my types and implement sort semantics for
    the type that really only apply in one spot.

    Your instinct is correct. If you need to sort a slice of something in one
    spot, put the type that implements sort.Interface nearby. If you find
    yourself using it again, then move it near the type.

    On Thursday, February 14, 2013 3:33:47 AM UTC+11, Patrick Mylund Nielsen
    wrote:
    Maybe, but I have to sympathize. It's harder than it should be, and not
    very intuitive, to e.g. sort by a field or index.

    On Wed, Feb 13, 2013 at 7:52 AM, Kamil Kisiel wrote:

    I hate to give the "read the documentation" kind of answers, but really,
    read the documentation :)

    This is almost exactly analogous to example given in the sort package:
    http://golang.org/**pkg/sort/#example_Interface<http://golang.org/pkg/sort/#example_Interface>

    Please have a read and try to understand it, if you're still having
    problems then ask a more specific question.

    On Tuesday, February 12, 2013 10:20:46 PM UTC-8, Constantine Vasil wrote:

    *I am having these structs:*

    *
    *

    *type* Record *struct* {

    ...

    Distance *string*

    * ...*

    Results *string*
    }

    *type* Response *struct* {

    ...

    Version *string*

    Records []Record

    }


    How to sort Response.Records array in ascending order based on
    Record.Distance?



    --
    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...@**googlegroups.com.

    For more options, visit https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
    .

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dan Kortschak at Feb 13, 2013 at 9:18 pm
    It's not really more complicated than defining a compar func for a C data type and it more clearly shows the association of the comparison to the type.
    On 14/02/2013, at 7:25 AM, "Daniel Bryan" wrote:

    Implementing sort.Interface is fine.. I just wish I could pass the funcs for Less(), Swap() and Len() into the sort function in a pinch, instead of having to go open the file with my types and implement sort semantics for the type that really only apply in one spot.
    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • John Nagle at Feb 13, 2013 at 10:53 pm

    On 2/13/2013 12:55 PM, Daniel Bryan wrote:
    Implementing sort.Interface is fine.. I just wish I could pass the funcs
    for Less(), Swap() and Len() into the sort function in a pinch, instead of
    having to go open the file with my types and implement sort semantics for
    the type that really only apply in one spot.
    What do you do when you need to be able to sort the same
    set of structs into different orders? (Use case: directory
    listing, which can be sorted by date, type, length, ascending,
    descending, etc.)

    John Nagle

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Daniel Bryan at Feb 13, 2013 at 11:26 pm

    On Wed, Feb 13, 2013 at 02:45:04PM -0800, John Nagle wrote:
    On 2/13/2013 12:55 PM, Daniel Bryan wrote:
    Implementing sort.Interface is fine.. I just wish I could pass the funcs
    for Less(), Swap() and Len() into the sort function in a pinch, instead of
    having to go open the file with my types and implement sort semantics for
    the type that really only apply in one spot.
    What do you do when you need to be able to sort the same
    set of structs into different orders? (Use case: directory
    listing, which can be sorted by date, type, length, ascending,
    descending, etc.)
    I tend to have a special struct type that has a swappable attribute for
    less. This means its Less method looks like this:

    func (p SortableThing) Less(i, j int) bool {
    return p.less(i, j)
    }


    The Sort() method on my main type boxes it in an instance of
    SortableThing and then defers to sort.Sort():

    func (p MyThing) Sort(sort_by string, less_func func(p MyThing, i, j int) bool) MyThing {
    sortable := Sortablething{MyThing, sort_by, less_func}
    return MyThing(sort.Sort(sortable))
    }

    And the final piece in the puzzle is a switch statement on sort_by that
    maps it to struct fields.

    If someone has a more sane way to do this I'd love to hear it..

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Peter at Feb 14, 2013 at 10:02 am
    There's a good example of this in the sort package:
    http://golang.org/pkg/sort/#example_Interface

    Basically, it creates types that embed your sortable structure, and
    implements a different Less() method for each kind of sorting you require.

    Hope that makes sense.
    On Wednesday, 13 February 2013 22:45:04 UTC, John Nagle wrote:
    On 2/13/2013 12:55 PM, Daniel Bryan wrote:
    Implementing sort.Interface is fine.. I just wish I could pass the funcs
    for Less(), Swap() and Len() into the sort function in a pinch, instead of
    having to go open the file with my types and implement sort semantics for
    the type that really only apply in one spot.
    What do you do when you need to be able to sort the same
    set of structs into different orders? (Use case: directory
    listing, which can be sorted by date, type, length, ascending,
    descending, etc.)

    John Nagle
    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • John Nagle at Feb 14, 2013 at 7:45 pm

    On 2/14/2013 2:02 AM, Peter wrote:
    There's a good example of this in the sort package:
    http://golang.org/pkg/sort/#example_Interface

    Basically, it creates types that embed your sortable structure, and
    implements a different Less() method for each kind of sorting you require.

    Hope that makes sense.
    That's rather clunky.

    A sort function which takes a comparison function with two
    Interface parameters would make more sense.

    John Nagle



    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dan Kortschak at Feb 14, 2013 at 8:32 pm
    This really depends on your perspective, and your proposal will be slower.
    On 15/02/2013, at 6:15 AM, "John Nagle" wrote:

    A sort function which takes a comparison function with two
    Interface parameters would make more sense.
    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • John Asmuth at Feb 14, 2013 at 10:57 pm
    Would the initial ordering come in as an interface{} as well? or perhaps it
    needs to be moved to a []interface{}? The current approach is quite
    effective, and sidesteps the issues that arise by not having any generics.
    On Thursday, February 14, 2013 2:44:41 PM UTC-5, John Nagle wrote:
    On 2/14/2013 2:02 AM, Peter wrote:
    There's a good example of this in the sort package:
    http://golang.org/pkg/sort/#example_Interface

    Basically, it creates types that embed your sortable structure, and
    implements a different Less() method for each kind of sorting you require.
    Hope that makes sense.
    That's rather clunky.

    A sort function which takes a comparison function with two
    Interface parameters would make more sense.

    John Nagle


    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • David DENG at Feb 14, 2013 at 2:16 am
    I just implemented a handy function and put it here:

    https://github.com/daviddengcn/go-villa/blob/master/sort.go

    copy the code or import "github.com/daviddengcn/go-villa" package to use it.

    David
    On Thursday, February 14, 2013 4:55:05 AM UTC+8, Daniel Bryan wrote:

    Implementing sort.Interface is fine.. I just wish I could pass the funcs
    for Less(), Swap() and Len() into the sort function in a pinch, instead of
    having to go open the file with my types and implement sort semantics for
    the type that really only apply in one spot.

    On Thursday, February 14, 2013 3:33:47 AM UTC+11, Patrick Mylund Nielsen
    wrote:
    Maybe, but I have to sympathize. It's harder than it should be, and not
    very intuitive, to e.g. sort by a field or index.

    On Wed, Feb 13, 2013 at 7:52 AM, Kamil Kisiel wrote:

    I hate to give the "read the documentation" kind of answers, but really,
    read the documentation :)

    This is almost exactly analogous to example given in the sort package:
    http://golang.org/pkg/sort/#example_Interface

    Please have a read and try to understand it, if you're still having
    problems then ask a more specific question.

    On Tuesday, February 12, 2013 10:20:46 PM UTC-8, Constantine Vasil wrote:

    *I am having these structs:*

    *
    *

    *type* Record *struct* {

    ...

    Distance *string*

    * ...*

    Results *string*
    }

    *type* Response *struct* {

    ...

    Version *string*

    Records []Record

    }


    How to sort Response.Records array in ascending order based on
    Record.Distance?



    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Kyle Lemons at Feb 13, 2013 at 4:17 pm
    The general form of any custom sort is essentially the same.

    At the call-site:
    sort.Sort(byDistance(r.Records))

    and by the struct type:
    type byDistance []Record
    func (v byDistance) Len() int { return len(v) }
    func (v byDistance) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
    func (v byDistance) Less(i, j int) bool { v[i].Distance < v[j].Distance }

    On Tue, Feb 12, 2013 at 10:20 PM, Constantine Vasil wrote:

    *I am having these structs:*

    *
    *

    *type* Record *struct* {

    ...

    Distance *string*

    * ...*

    Results *string*
    }

    *type* Response *struct* {

    ...

    Version *string*

    Records []Record

    }


    How to sort Response.Records array in ascending order based on
    Record.Distance?



    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Patrick Mylund Nielsen at Feb 13, 2013 at 4:31 pm
    This is the best solution.

    For a quick and dirty solution (i.e. slower, as it uses reflect), you can
    do:

    import "github.com/pmylund/sortutil"

    sortutil.AscByField(res.Records, "Distance")

    On Wed, Feb 13, 2013 at 5:16 PM, Kyle Lemons wrote:

    The general form of any custom sort is essentially the same.

    At the call-site:
    sort.Sort(byDistance(r.Records))

    and by the struct type:
    type byDistance []Record
    func (v byDistance) Len() int { return len(v) }
    func (v byDistance) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
    func (v byDistance) Less(i, j int) bool { v[i].Distance < v[j].Distance }

    On Tue, Feb 12, 2013 at 10:20 PM, Constantine Vasil wrote:

    *I am having these structs:*

    *
    *

    *type* Record *struct* {

    ...

    Distance *string*

    * ...*

    Results *string*
    }

    *type* Response *struct* {

    ...

    Version *string*

    Records []Record

    }


    How to sort Response.Records array in ascending order based on
    Record.Distance?



    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Carlos Castillo at Feb 15, 2013 at 4:45 am
    Since everyone seems to be promoting their own packages, let me do the
    same... :-)

    My code (github.com/cookieo9/go-misc/slice) creates a wrapper around an
    arbitrary slice which provides the Len & Swap methods, and all you need to
    do is provide the comparator for two elements.

    eg: http://play.golang.org/p/EctrGHuVf7

    Still, actually implementing an instance of sort.Interface and using
    sort.Sort is faster and not that big a deal. I barely use this code at all.

    On Wednesday, February 13, 2013 8:31:31 AM UTC-8, Patrick Mylund Nielsen
    wrote:
    This is the best solution.

    For a quick and dirty solution (i.e. slower, as it uses reflect), you can
    do:

    import "github.com/pmylund/sortutil"

    sortutil.AscByField(res.Records, "Distance")


    On Wed, Feb 13, 2013 at 5:16 PM, Kyle Lemons <[email protected]<javascript:>
    wrote:
    The general form of any custom sort is essentially the same.

    At the call-site:
    sort.Sort(byDistance(r.Records))

    and by the struct type:
    type byDistance []Record
    func (v byDistance) Len() int { return len(v) }
    func (v byDistance) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
    func (v byDistance) Less(i, j int) bool { v[i].Distance < v[j].Distance }


    On Tue, Feb 12, 2013 at 10:20 PM, Constantine Vasil <[email protected]<javascript:>
    wrote:
    *I am having these structs:*

    *
    *

    *type* Record *struct* {

    ...

    Distance *string*

    * ...*

    Results *string*
    }

    *type* Response *struct* {

    ...

    Version *string*

    Records []Record

    }


    How to sort Response.Records array in ascending order based on
    Record.Distance?



    --
    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 [email protected] <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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 [email protected] <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Constantine Vasil at Feb 14, 2013 at 3:01 am
    Thank you for the link with example but of course I

    was reading it. I was in search for better general solution.


    I got it working following the example. It used pointers, my code

    do not and I was not sure it will work initially. That is

    why I asked here in the forum if there is a better solution.


    Having experience, say with VB.NET, some things are

    easier to do in VB.NET. Probably Golang could borrow ideas

    for better usability.



    I think Golang solution is low level in a good way and flexible but

    sorting for example is highly un-intuitive. The need to write own

    functions is in-flexible and time consuming.


    For strings Len, Swap and Less should be the same for all String

    related sortings, there is no need to be required to write my own.

    Also for each struct element I need to write a separate sorting function.


    The bottom line is that sorting an array of struct by struct element

    is very common and should be easier.


    I see Golang as very useful language that is gives so much flexibility.

    From other side when a project is big there is no time to delve deeper

    and one needs ready examples how to do things, There are only three books

    on Golang where to search for examples of which only one

    "The way to Go" from Ivo Balbaert I am referencing constantly.


    Here is the code I got:

    /////////////////////////////////////////////////////////////////////////////////

    // Sorting ResRecords

    /////////////////////////////////////////////////////////////////////////////////

    *type* ResRecords []Record


    *func* (s ResRecords) Len() *int* {

    *return* *len*(s)

    }


    *func* (s ResRecords) Swap(i, j *int*) {

    s[i], s[j] = s[j], s[i]

    }


    // ByDistance implements sort.Interface by providing Less and using the Len
    and

    // Swap methods of the embedded ResRecords value.

    *type* ByDistance *struct*{ ResRecords }


    *func* (s ByDistance) Less(i, j *int*) *bool* {

    *return* s.ResRecords[i].Distance < s.ResRecords[j].Distance

    }


    /////////////////////////////////////////////////////////////////////////////////

    *
    *

    *Usage:*

    *
    *

    *var* response Res

    sort.Sort(ByDistance{response.Records})



    On Tuesday, February 12, 2013 10:20:46 PM UTC-8, Constantine Vasil wrote:

    *I am having these structs:*

    *
    *

    *type* Record *struct* {

    ...

    Distance *string*

    * ...*

    Results *string*
    }

    *type* Response *struct* {

    ...

    Version *string*

    Records []Record

    }


    How to sort Response.Records array in ascending order based on
    Record.Distance?


    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Constantine Vasil at Feb 14, 2013 at 3:10 am
    *Just brainstorming. *

    *
    *

    *If it could be done like this automatically it would be*

    *a dream:*

    *==================================*

    *var* response Res
    response.Records = sort(Res.Records[0].Record.Distance)
    *==================================*
    *
    *
    Referencing the struct element on which to sort on can be something
    like when one is accessing REST or XML elements and
    applying a function, in this instance a "sort".

    This is just a brainstorming I am not sure if it can be done automatically.

    On Wednesday, February 13, 2013 7:01:53 PM UTC-8, Constantine Vasil wrote:

    Thank you for the link with example but of course I

    was reading it. I was in search for better general solution.


    I got it working following the example. It used pointers, my code

    do not and I was not sure it will work initially. That is

    why I asked here in the forum if there is a better solution.


    Having experience, say with VB.NET, some things are

    easier to do in VB.NET. Probably Golang could borrow ideas

    for better usability.



    I think Golang solution is low level in a good way and flexible but

    sorting for example is highly un-intuitive. The need to write own

    functions is in-flexible and time consuming.


    For strings Len, Swap and Less should be the same for all String

    related sortings, there is no need to be required to write my own.

    Also for each struct element I need to write a separate sorting function.


    The bottom line is that sorting an array of struct by struct element

    is very common and should be easier.


    I see Golang as very useful language that is gives so much flexibility.

    From other side when a project is big there is no time to delve deeper

    and one needs ready examples how to do things, There are only three books

    on Golang where to search for examples of which only one

    "The way to Go" from Ivo Balbaert I am referencing constantly.


    Here is the code I got:


    /////////////////////////////////////////////////////////////////////////////////

    // Sorting ResRecords


    /////////////////////////////////////////////////////////////////////////////////

    *type* ResRecords []Record


    *func* (s ResRecords) Len() *int* {

    *return* *len*(s)

    }


    *func* (s ResRecords) Swap(i, j *int*) {

    s[i], s[j] = s[j], s[i]

    }


    // ByDistance implements sort.Interface by providing Less and using the
    Len and

    // Swap methods of the embedded ResRecords value.

    *type* ByDistance *struct*{ ResRecords }


    *func* (s ByDistance) Less(i, j *int*) *bool* {

    *return* s.ResRecords[i].Distance < s.ResRecords[j].Distance

    }



    /////////////////////////////////////////////////////////////////////////////////

    *
    *

    *Usage:*

    *
    *

    *var* response Res

    sort.Sort(ByDistance{response.Records})



    On Tuesday, February 12, 2013 10:20:46 PM UTC-8, Constantine Vasil wrote:

    *I am having these structs:*

    *
    *

    *type* Record *struct* {

    ...

    Distance *string*

    * ...*

    Results *string*
    }

    *type* Response *struct* {

    ...

    Version *string*

    Records []Record

    }


    How to sort Response.Records array in ascending order based on
    Record.Distance?


    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Patrick Mylund Nielsen at Feb 14, 2013 at 10:06 am
    It can:

    import "github.com/pmylund/sortutil"
    sortutil.AscByField(res.Records, "Distance")

    It's just less efficient.

    On Thu, Feb 14, 2013 at 4:10 AM, Constantine Vasil wrote:

    *Just brainstorming. *

    *
    *

    *If it could be done like this automatically it would be*

    *a dream:*

    *==================================*

    *var* response Res
    response.**Records = sort(Res.**Records[0].Record.Distance)
    *==================================*
    *
    *
    Referencing the struct element on which to sort on can be something
    like when one is accessing REST or XML elements and
    applying a function, in this instance a "sort".

    This is just a brainstorming I am not sure if it can be done automatically.

    On Wednesday, February 13, 2013 7:01:53 PM UTC-8, Constantine Vasil wrote:

    Thank you for the link with example but of course I

    was reading it. I was in search for better general solution.


    I got it working following the example. It used pointers, my code

    do not and I was not sure it will work initially. That is

    why I asked here in the forum if there is a better solution.


    Having experience, say with VB.NET, some things are

    easier to do in VB.NET. Probably Golang could borrow ideas

    for better usability.



    I think Golang solution is low level in a good way and flexible but

    sorting for example is highly un-intuitive. The need to write own

    functions is in-flexible and time consuming.


    For strings Len, Swap and Less should be the same for all String

    related sortings, there is no need to be required to write my own.

    Also for each struct element I need to write a separate sorting function.


    The bottom line is that sorting an array of struct by struct element

    is very common and should be easier.


    I see Golang as very useful language that is gives so much flexibility.

    From other side when a project is big there is no time to delve deeper

    and one needs ready examples how to do things, There are only three books

    on Golang where to search for examples of which only one

    "The way to Go" from Ivo Balbaert I am referencing constantly.


    Here is the code I got:

    //////////////////////////////**//////////////////////////////**
    /////////////////////

    // Sorting ResRecords

    //////////////////////////////**//////////////////////////////**
    /////////////////////

    *type* ResRecords []Record


    *func* (s ResRecords) Len() *int* {

    *return* *len*(s)

    }


    *func* (s ResRecords) Swap(i, j *int*) {

    s[i], s[j] = s[j], s[i]

    }


    // ByDistance implements sort.Interface by providing Less and using the
    Len and

    // Swap methods of the embedded ResRecords value.

    *type* ByDistance *struct*{ ResRecords }


    *func* (s ByDistance) Less(i, j *int*) *bool* {

    *return* s.ResRecords[i].Distance < s.ResRecords[j].Distance

    }


    //////////////////////////////**//////////////////////////////**
    /////////////////////

    *
    *

    *Usage:*

    *
    *

    *var* response Res

    sort.Sort(ByDistance{response.**Records})



    On Tuesday, February 12, 2013 10:20:46 PM UTC-8, Constantine Vasil wrote:

    *I am having these structs:*

    *
    *

    *type* Record *struct* {

    ...

    Distance *string*

    * ...*

    Results *string*
    }

    *type* Response *struct* {

    ...

    Version *string*

    Records []Record

    }


    How to sort Response.Records array in ascending order based on
    Record.Distance?



    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Kevin Gillette at Feb 15, 2013 at 5:31 am
    I'm not convinced that Kyle Lemon's suggestion, e.g. the canonical,
    idiomatic approach, is "hard". It's only a trio of very simple,
    side-effect-free functions, and compared to reflect-based alternatives, is
    type-safe will usually be faster. Compared to hypothetical meta-programming
    options, there aren't any unicorns.

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Daniel Bryan at Feb 15, 2013 at 6:06 am
    The issue for me is more that if you want to sort in different parts of
    your code on different values you have to jump through a non-idiomatic hoop
    or have multiple implementations of the interface.
    On Feb 15, 2013 4:31 PM, "Kevin Gillette" wrote:

    I'm not convinced that Kyle Lemon's suggestion, e.g. the canonical,
    idiomatic approach, is "hard". It's only a trio of very simple,
    side-effect-free functions, and compared to reflect-based alternatives, is
    type-safe will usually be faster. Compared to hypothetical meta-programming
    options, there aren't any unicorns.

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Kevin Gillette at Feb 15, 2013 at 7:50 am
    Having multiple ways of sorting the same slice actually reduces the code
    per sort: in particular, only the Less method needs to be redefined (along
    with a throwaway type) for each sort strategy across the same underlying
    type. See: <http://play.golang.org/p/rWO6jbev1j>

    The amount of code involved is arguably not significantly more than using
    callback approach.
    On Thursday, February 14, 2013 11:06:23 PM UTC-7, Daniel Bryan wrote:

    The issue for me is more that if you want to sort in different parts of
    your code on different values you have to jump through a non-idiomatic hoop
    or have multiple implementations of the interface.
    On Feb 15, 2013 4:31 PM, "Kevin Gillette" wrote:

    I'm not convinced that Kyle Lemon's suggestion, e.g. the canonical,
    idiomatic approach, is "hard". It's only a trio of very simple,
    side-effect-free functions, and compared to reflect-based alternatives, is
    type-safe will usually be faster. Compared to hypothetical meta-programming
    options, there aren't any unicorns.

    --
    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 [email protected] <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Joshua Marsh at Feb 15, 2013 at 3:55 pm

    On Thursday, February 14, 2013 11:06:23 PM UTC-7, Daniel Bryan wrote:

    The issue for me is more that if you want to sort in different parts of
    your code on different values you have to jump through a non-idiomatic hoop
    or have multiple implementations of the interface.
    I had problems with the sort interface at first as well. The problem though
    was that my mindset was stuck in C. It I were trying to translate code
    directly from C to Go, I'd be using interface{} and comparison functions.
    That's not the Go way though. Interfaces and inline structs are the Go
    equivalent. After changing my mindset, I have to say I enjoy the Go
    approach much more.


    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • John Nagle at Feb 15, 2013 at 8:21 pm

    On 2/14/2013 10:06 PM, Daniel Bryan wrote:
    The issue for me is more that if you want to sort in different parts of
    your code on different values you have to jump through a non-idiomatic hoop
    or have multiple implementations of the interface.
    Here's an simple approach which allows writing a generic
    sort where a compare function is passed as a parameter.

    http://play.golang.org/p/LyOtZC5Paj

    No need for wrapping, or reflection.

    However, the line

    func (tab dirtab) Index(n int) interface{} { return tab[n] }

    implies that an interface object has to be created each
    time an element is accessed. If that's done on the stack,
    it's not too bad, but if it's done on the heap, there's
    a big allocation cost.

    John Nagle

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dan Kortschak at Feb 15, 2013 at 8:34 pm
    It saves one type definition (one line) in the client code at the cost of two type assertions per comparison. Have you looked at what that does to performance?
    On 16/02/2013, at 6:51 AM, "John Nagle" wrote:

    No need for wrapping, or reflection.
    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 13, '13 at 6:20a
activeFeb 15, '13 at 8:34p
posts25
users13
websitegolang.org

People

Translate

site design / logo © 2023 Grokbase