// MySort IS NOT USING AN INTERFACE TO SORT
MySort IS NOT USING AN INTERFACE TO SORT
// MySort IS NOT USING AN INTERFACE TO SORT
func MySort(People []Person) {
	AgeToPeople := make(map[int][]Person)
	for _, p := range People {
		_, ok := AgeToPeople[p.Age]
		if !ok {
			AgeToPeople[p.Age] = []Person{p}
		} else {
			temp := AgeToPeople[p.Age]
			AgeToPeople[p.Age] = append(temp, p) 
		}
	}
	var Ages []int
	for age := range AgeToPeople {
		Ages = append(Ages, age)
	}
	sort.Ints(Ages)
	var PeopleSortedByAge []Person
	for _, age := range Ages {
		temp := AgeToPeople[age]
		for _, p := range temp {
			PeopleSortedByAge = append(PeopleSortedByAge, p)
		}
	}
	fmt.Printf("Sorted by age Listing: %v \n", PeopleSortedByAge)
	PeopleSortedByAgeSubset := PeopleSortedByAge[:3]
	fmt.Printf("Stable Subset of Sorted by age: %v \n", PeopleSortedByAgeSubset)
}
func MySort ( People [ ] Person ) { AgeToPeople := make ( map [ int ] [ ] Person ) for _ , p := range People { _ , ok := AgeToPeople [ p . Age ] if ! ok { AgeToPeople [ p . Age ] = [ ] Person { p } } else { temp := AgeToPeople [ p . Age ] AgeToPeople [ p . Age ] = append ( temp , p ) } } var Ages [ ] int for age := range AgeToPeople { Ages = append ( Ages , age ) } sort . Ints ( Ages ) var PeopleSortedByAge [ ] Person for _ , age := range Ages { temp := AgeToPeople [ age ] for _ , p := range temp { PeopleSortedByAge = append ( PeopleSortedByAge , p ) } } fmt . Printf ( " \n " , PeopleSortedByAge ) PeopleSortedByAgeSubset := PeopleSortedByAge [ : 3 ] fmt . Printf ( " \n " , PeopleSortedByAgeSubset ) }
MySort IS NOT USING AN INTERFACE TO SORT
MySort IS NOT USING AN INTERFACE TO SORT
// Example of manually sorting using extra primitive data structures // handle the edge case of not yet having a list entry in the map // this maintains the pre-existing order on duplicates // Sort by a property and then dereference the lookup map // get the unique list of ages (each map key is unique) // Optionally create a subset of the "lowest N members" // this could be more efficient if it was applied during the PeopleSortedByAge loop