|
|
Descriptionsort: add an example showing sorting struct by different keys
Patch Set 1 #Patch Set 2 : diff -r c28d9e5f42ff https://code.google.com/p/go #
Total comments: 1
Patch Set 3 : diff -r c28d9e5f42ff https://code.google.com/p/go #Patch Set 4 : diff -r 1e891021e069 https://go.googlecode.com/hg/ #
Total comments: 2
Patch Set 5 : diff -r faf5a57def8c https://go.googlecode.com/hg/ #Patch Set 6 : diff -r faf5a57def8c https://go.googlecode.com/hg/ #MessagesTotal messages: 22
Hello golang-dev@googlegroups.com, I'd like you to review this change to https://code.google.com/p/go
Sign in to reply to this message.
Hello golang-dev@googlegroups.com (cc: golang-dev@googlegroups.com), Please take another look.
Sign in to reply to this message.
https://codereview.appspot.com/7376058/diff/3001/src/pkg/sort/example_keys_te... File src/pkg/sort/example_keys_test.go (right): https://codereview.appspot.com/7376058/diff/3001/src/pkg/sort/example_keys_te... src/pkg/sort/example_keys_test.go:49: s.planets = planets splitting this setup seems a little odd, and means you can't use Sort concurrently, nor even call String before sorting. The simplest solution would be two types (a func and a func+slice).
Sign in to reply to this message.
I don't understand your proposal. -rob
Sign in to reply to this message.
Hello golang-dev@googlegroups.com, dsymonds@golang.org (cc: golang-dev@googlegroups.com), Please take another look.
Sign in to reply to this message.
On Wed, Feb 27, 2013 at 10:47 AM, Rob Pike <r@golang.org> wrote: > I don't understand your proposal. Have type planetComparer func(p1, p2 *Planet) bool which is returned by the By function. It has a Sort method that takes a []Planet as you currently have it, but then uses a second type, something like type planetSorter struct { less func(p1, p2 *Planet) bool s []Planet } on which the Len/Less/Swap methods are hung. The Sort method creates a new instance of that struct each call, and passes it to sort.Sort to do the work.
Sign in to reply to this message.
That's a lot of boilerplate. I was trying to write something small and pretty. Let me think about it some more. -rob
Sign in to reply to this message.
Hello golang-dev@googlegroups.com, dsymonds@golang.org (cc: golang-dev@googlegroups.com), Please take another look.
Sign in to reply to this message.
I think I semi-independently came up with your approach, and it's slightly more magical but a good example. Plus methods on functions are always fun. -rob
Sign in to reply to this message.
LGTM that's nicer.
Sign in to reply to this message.
*** Submitted as https://code.google.com/p/go/source/detail?r=2bf8d07c14c1 *** sort: add an example showing sorting struct by different keys R=golang-dev, dsymonds CC=golang-dev https://codereview.appspot.com/7376058
Sign in to reply to this message.
Message was sent while issue was closed.
https://codereview.appspot.com/7376058/diff/14001/src/pkg/sort/example_keys_t... File src/pkg/sort/example_keys_test.go (right): https://codereview.appspot.com/7376058/diff/14001/src/pkg/sort/example_keys_t... src/pkg/sort/example_keys_test.go:63: // ExampleSortKeys demonstrates a technique for sorting a struct type using programmable sort criteria. Example_SortKeys ? https://codereview.appspot.com/7376058/diff/14001/src/pkg/sort/example_keys_t... src/pkg/sort/example_keys_test.go:64: func Example_sortKeys() { capital S?
Sign in to reply to this message.
According to package testing, it must be lower case or godoc will insist it be an exported identifier in the package. -rob
Sign in to reply to this message.
Might there be a clever way to chain these for multiple keys? (by X and then by Y) On Tue, Feb 26, 2013 at 9:05 PM, <adg@golang.org> wrote: > > https://codereview.appspot.**com/7376058/diff/14001/src/** > pkg/sort/example_keys_test.go<https://codereview.appspot.com/7376058/diff/14001/src/pkg/sort/example_keys_test.go> > File src/pkg/sort/example_keys_**test.go (right): > > https://codereview.appspot.**com/7376058/diff/14001/src/** > pkg/sort/example_keys_test.go#**newcode63<https://codereview.appspot.com/7376058/diff/14001/src/pkg/sort/example_keys_test.go#newcode63> > src/pkg/sort/example_keys_**test.go:63: // ExampleSortKeys demonstrates a > technique for sorting a struct type using programmable sort criteria. > Example_SortKeys ? > > https://codereview.appspot.**com/7376058/diff/14001/src/** > pkg/sort/example_keys_test.go#**newcode64<https://codereview.appspot.com/7376058/diff/14001/src/pkg/sort/example_keys_test.go#newcode64> > src/pkg/sort/example_keys_**test.go:64: func Example_sortKeys() { > capital S? > > > https://codereview.appspot.**com/7376058/<https://codereview.appspot.com/7376... > > -- > > ---You received this message because you are subscribed to the Google > Groups "golang-dev" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-dev+unsubscribe@**googlegroups.com<golang-dev%2Bunsubscribe@googlegrou... > . > For more options, visit https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/o... > . > > > -- Michael T. Jones | Chief Technology Advocate | mtj@google.com | +1 650-335-5765
Sign in to reply to this message.
I forgot about that. On 27 February 2013 13:12, Rob Pike <r@golang.org> wrote: > According to package testing, it must be lower case or godoc will > insist it be an exported identifier in the package. > > -rob >
Sign in to reply to this message.
On Tue, Feb 26, 2013 at 6:14 PM, Michael Jones <mtj@google.com> wrote: > Might there be a clever way to chain these for multiple keys? (by X and then > by Y) I'm sure there is. -rob
Sign in to reply to this message.
On 27 February 2013 13:14, Michael Jones <mtj@google.com> wrote: > Might there be a clever way to chain these for multiple keys? (by X and > then by Y) Sure: nameAndDistance := func(p1, p2 *Planet) bool { if p1.name == p2.name { return distance(p1, p2) } return name(p1, p2) }
Sign in to reply to this message.
Ooh, so that should be in the example. ;-) On Tue, Feb 26, 2013 at 11:59 PM, Andrew Gerrand <adg@golang.org> wrote: > > On 27 February 2013 13:14, Michael Jones <mtj@google.com> wrote: > >> Might there be a clever way to chain these for multiple keys? (by X and >> then by Y) > > > Sure: > > nameAndDistance := func(p1, p2 *Planet) bool { > if p1.name == p2.name { > return distance(p1, p2) > } > return name(p1, p2) > } > > -- Michael T. Jones | Chief Technology Advocate | mtj@google.com | +1 650-335-5765
Sign in to reply to this message.
The dataset doesn't really lend itself to being sorted in multiple dimensions because none of rows have common values in any field. On 27 February 2013 16:03, Michael Jones <mtj@google.com> wrote: > Ooh, so that should be in the example. ;-) > > > On Tue, Feb 26, 2013 at 11:59 PM, Andrew Gerrand <adg@golang.org> wrote: > >> >> On 27 February 2013 13:14, Michael Jones <mtj@google.com> wrote: >> >>> Might there be a clever way to chain these for multiple keys? (by X and >>> then by Y) >> >> >> Sure: >> >> nameAndDistance := func(p1, p2 *Planet) bool { >> if p1.name == p2.name { >> return distance(p1, p2) >> } >> return name(p1, p2) >> } >> >> > > > -- > Michael T. Jones | Chief Technology Advocate | mtj@google.com | +1 > 650-335-5765 >
Sign in to reply to this message.
I'd use a slice of functions, and call the next element of the slice only if the first function claims equality. An exercise for the reader to construct. -rob
Sign in to reply to this message.
On 27 February 2013 18:22, Rob Pike <r@golang.org> wrote: > the first function claims equality. How does that work? !fn(a, b) && !fn(b, a) ?
Sign in to reply to this message.
|