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.