TutorialEdge

Challenge 03 - Sorting By Price

Welcome, everyone to the discussion thread for the third Go challenge posted to the site :tada:

Feel free to show us how you solved the challenge by posting your solutions below!

Solution:

package main

import (
	"fmt"
	"sort"
)

type Flight struct {
	Origin      string
	Destination string
	Price       int
}

type ByPrice []Flight

func (p ByPrice) Len() int {
	return len(p)
}

func (p ByPrice) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}

func (p ByPrice) Less(i, j int) bool {
	return p[i].Price > p[j].Price
}

func SortByPrice(flights []Flight) []Flight {
	sort.Sort(ByPrice(flights))
	return flights
}

func main() {
	flights := []Flight{
		Flight{Price: 30},
		Flight{Price: 20},
		Flight{Price: 50},
		Flight{Price: 1000},
	}

	sort.Sort(ByPrice(flights))

	fmt.Println(flights)
}

Hi @Elliot, in the challenge, the test passes, without editing the code at all

Hi @kevinschweikert - Good catch! I’ve updated the test and it should be going live now! thanks for pointing this out! :muscle:

1 Like

Shouldn`t the Sorting in Go with the Sort Package part of the challenge 3 be a clickable link for the easiness of the access?

You are correct! Thanks for pointing that out!

The reason this happened is due to the fact I upgraded the version of Hugo I use to build the site and this has changed the underlying markdown renderer. I must have missed this particular use-case when I was testing to ensure it works!

1 Like

I think this challenge supposed to sort from Lowest to Highest.

Somehow, task being passed vise-verse Highest to Lowest

// SortByPrice sorts flights from lower to highest
func SortByPrice(f []Flight) []Flight {
 n := len(f)-1
 sw := true

 for sw {
  sw = false
  for i:= 0; i < n; i++{
   if f[i].Price < f[i+1].Price {
    f[i], f[i+1] = f[i+1], f[i]
    sw = true
   }
  }
  n--
 }
 return f
}

Welcome to the forums!

You are correct, I’ll fix that asap!

Nice implementation for the challenge btw!

I just wonder in your solution you dont even call this function SortByPrice and I make it work but test is not pass

1 Like

My solution works, but the tests fail, why?

package main

import (
  "fmt"
  "sort"
)
// Flight - a struct that
// contains information about flights
type Flight struct {
  Origin string
  Destination string
  Price int
}

// SortByPrice sorts flights from lower to highest
func SortByPrice(flights []Flight) []Flight {
	sort.Slice(flights, func(i, j int) bool { return flights[i].Price < flights[j].Price })

	return flights
}

func printFlights(flights []Flight) {
  for _, flight := range flights {
    fmt.Printf("Origin: %s\nDestination: %s\nPrice: %d\n\n", flight.Origin, flight.Destination, flight.Price)
  }
}

func main() {
  // an empty slice of flights
	flights := []Flight{
		Flight{Price: 10},
		Flight{Price: 20},
		Flight{Price: 50},
		Flight{Price: 15},
	}

  sortedList := SortByPrice(flights)
  printFlights(sortedList)
}

Hi Balisitg,

Welcome to the community!

The output from the main function is really to help debug and show how to call the function. The test function itself only tests the SortByPrice function

Here is the source code for the test being run against this challenge:

package main

      import (
        "fmt"
        "testing"
      )

      func TestSortByPrice(t *testing.T) {
        flights := []Flight{
          Flight{Price: 30},
          Flight{Price: 20},
          Flight{Price: 50},
          Flight{Price: 1000},
        }

        expected := []Flight{
          Flight{Price: 1000},
          Flight{Price: 50},
          Flight{Price: 30},
          Flight{Price: 20},
        }

        result := SortByPrice(flights)

        for i := 0; i < len(expected); i++ {
          fmt.Println(result[i].Price)
          fmt.Println(expected[i].Price)
          if result[i].Price != expected[i].Price {
            t.Fail()
          }
        }

      }

Welcome to the community!

There was a typo in the comments for sort by price, it should say highest to lowest!

Your solution works and I’ve updated the typo which should be going live now!

1 Like