From 47f84ed5614412692a6318736627a7711fd1e5f3 Mon Sep 17 00:00:00 2001 From: k1ng87 Date: Mon, 28 May 2018 22:36:21 -0700 Subject: [PATCH] added the solution...solution seemed to be missing --- .../003_first_and_last_name_solution/main.go | 40 ++++++++++++++++--- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/001_bubble_sort/practice_problems/003_first_and_last_name_solution/main.go b/001_bubble_sort/practice_problems/003_first_and_last_name_solution/main.go index 1ce9e22..806a1f7 100644 --- a/001_bubble_sort/practice_problems/003_first_and_last_name_solution/main.go +++ b/001_bubble_sort/practice_problems/003_first_and_last_name_solution/main.go @@ -1,6 +1,9 @@ package main -import "fmt" +import ( + "fmt" + "strings" +) type User struct { FirstName string @@ -48,14 +51,41 @@ func main() { } fmt.Println("Unsorted:", users) - bubbleSort(users) + + for i := 0; i < len(users); i++ { + bubbleSort(users[:len(users)-i]) + } + fmt.Println("Sorted: ", users) + } func bubbleSort(users []User) { - // Implement this and any other functions you may need - // If you are writing Go code, you can access the first - // and last name like so: fmt.Println(users[0].FirstName) fmt.Println(users[0].LastName) + + var lngth int = len(users) + var indx1 int = 0 + var indx2 int = 1 + + for indx2 < lngth { + var val1 User = users[indx1] + var val2 User = users[indx2] + + if asc(users[indx1], users[indx2]) { + users[indx1] = val2 + users[indx2] = val1 + } + + indx1++ + indx2++ + } + +} + +func asc(first User, second User) bool { + if strings.ToLower(first.LastName + first.FirstName) < strings.ToLower(second.LastName + second.FirstName) { + return true + } + return false }