Advertisement
AntonioVillanueva

Ejercicio 10 Go ordenar array strings

Apr 19th, 2023
913
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.05 KB | None | 0 0
  1. /*
  2. https://gocoding.org/es/ejercicios-de-pr%C3%A1ctica-de-golang/
  3. 10. Escriba un programa que tome una lista de nombres como entrada y los ordene alfabéticamente.
  4. */
  5.  
  6. package main
  7.  
  8. import (
  9.     "fmt"
  10.     "strings"
  11. )
  12.  
  13. // Compara la primera letra de dos nombres
  14. func isLower(a string, b string) bool {
  15.  
  16.     //Evita una comparacion mayusculas minusculas
  17.     a = strings.ToLower(a)
  18.     b = strings.ToLower(b)
  19.  
  20.     if a < b { //Si a es menor que b true
  21.         return true
  22.     }
  23.     return false
  24. }
  25.  
  26. // Cambia dos nombres por sus indices
  27. func swap(nombres *[]string, a int, b int) {
  28.     tmp := (*nombres)[a]
  29.     (*nombres)[a] = (*nombres)[b]
  30.     (*nombres)[b] = tmp
  31. }
  32.  
  33. // Lo mas ligero flota lo pesado va al fonodo
  34. func burbuja(nombres []string) []string {
  35.  
  36.     for ia, a := range nombres {
  37.         for ib, b := range nombres {
  38.  
  39.             if isLower(a, b) { //Si a<b los intercambia
  40.                 swap(&nombres, ia, ib)
  41.             }
  42.         }
  43.     }
  44.     return nombres
  45. }
  46.  
  47. func main() {
  48.     nombres := []string{"icaro ", "tony", "juan", "luis", "Nathalie", "Renato", "Yolanda", "Trini"}
  49.     fmt.Println(burbuja(nombres))
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement