Hello Srinivas

The traditional way to achieve this in go (as showcased here 
<https://golang.org/pkg/sort/#example_> and here 
<http://www.programming-idioms.org/idiom/28/sort-by-a-property/160/go>) is, 
to :
- declare a named type of slice
- implement 3 methods Len, Less, Swap 
<https://golang.org/pkg/sort/#Interface>
- call sort.Sort <https://golang.org/pkg/sort/#Sort>

(each attribute age, name, sal, needs its own type declaration and 3 method 
implementations).

Since Go 1.8, we can achieve basically the same with much less boilerplate 
code (as showcased here <https://golang.org/pkg/sort/#example_Slice> and 
here <http://www.programming-idioms.org/idiom/28/sort-by-a-property/1936/go>) 
:
- implement 1 func "less" having type  func(i, j int) bool
- call sort.Slice <https://golang.org/pkg/sort/#Slice>

(each attribute age, name, sal, needs its own "less" func).

I suggest the latter.
HTH
Valentin

On Friday, July 21, 2017 at 3:17:29 PM UTC+2, Srinivas Kandula wrote:
>
> Hi,
> I have below requirement, could any one suggest on how i can achieve in go 
> lang using https://golang.org/pkg/sort/.
> - I have user defined data structure called Employee with attributes 
> "age", name and sal.  I want to write three different go functions that 
> sorts based on age, name and sal.
> example:-
>   sortEmployeeByAge(employeelist)
>   sortEmployeeByName(employeelist)
>   sortEmployeeBySal(employeelist)
>
> Thanks,
> Srinivas K
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to