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.
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.
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})
Just brainstorming.
If it could be done like this automatically it would be
a dream:
==================================
var response Res
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'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.
--
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.