🚀 Get 25% off access to all my premium courses - use discount code FUNCMAIN at checkout - view the pricing page now!

Challenge 03 - Sorting Flights by Price

In this challenge, you are going to be attempting to sort a list of Flights based on their price from highest to lowest.

You will have to implement the SortByPrice function that takes in a slice of type Flight and returns the sorted list of Flights.

In order to help you see what is going on, you have been provided a very quick printFlights function which you can use to print the flights out.

Helpful Article

See the Solution

View Solution
package main

import (

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 {
	return flights

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



Further Reading:

If you like this challenge then you may also appreciate some of the following articles on the site:

Other Challenges