Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Task 5 — Map Sorting

Objective

Create two functions that sort data stored in a map[string]int.

The first function must sort the data based on the map keys.

The second function must sort the data based on the values associated with those keys.

Because a Go map does not preserve iteration order, both functions must return the sorted result as an ordered list of structures.

Input

The input map contains the following values:

data := map[string]int{
    "David": 40,
    "Paul":  20,
    "Bill":  30,
    "Fred":  50,
    "Alex":  10,
}

Each entry contains:

string key -> integer value

For example:

"David" -> 40
"Paul"  -> 20
"Bill"  -> 30
"Fred"  -> 50
"Alex"  -> 10

Result Type

Define a structure that represents one map entry:

type MapItem struct {
    Key   string
    Value int
}

Both sorting functions must return:

[]MapItem

The order of the elements inside the returned slice represents the sorted order.

Function 1 — Sort by Keys

Create a function that sorts the input map entries based on their keys.

The keys must be sorted in ascending alphabetical order.

For the provided input, the expected key order is:

Alex
Bill
David
Fred
Paul

Expected Result

[]MapItem{
    {Key: "Alex", Value: 10},
    {Key: "Bill", Value: 30},
    {Key: "David", Value: 40},
    {Key: "Fred", Value: 50},
    {Key: "Paul", Value: 20},
}

Function 2 — Sort by Values

Create a second function that sorts the input map entries based on their integer values.

The values must be sorted in ascending order.

For the provided input, the expected value order is:

10
20
30
40
50

Expected Result

[]MapItem{
    {Key: "Alex", Value: 10,},
    {Key: "Paul", Value: 20},
    {Key: "Bill", Value: 30},
    {Key: "David", Value: 40},
    {Key: "Fred", Value: 50},
}

Requirements

Create two separate functions.

The first function must:

  1. receive the input map[string]int
  2. convert the map entries into sortable data
  3. sort the entries by Key
  4. return the result as []MapItem

The second function must:

  1. receive the input map[string]int
  2. convert the map entries into sortable data
  3. sort the entries by Value
  4. return the result as []MapItem

The association between every key and its original value must be preserved.

Sorting Rules

Key Sorting

Key sorting must use ascending alphabetical order.

For example:

Alex
Bill
David
Fred
Paul

Value Sorting

Value sorting must use ascending numeric order.

For example:

10
20
30
40
50

Duplicate Values

If multiple map entries contain the same integer value, their relative order must be deterministic.

Use the key as a secondary sorting criterion.

For example, if the input contains:

map[string]int{
    "David": 20,
    "Alex":  20,
    "Paul":  10,
}

sorting by value should produce:

[]MapItem{
    {Key: "Paul", Value: 10},
    {Key: "Alex", Value: 20},
    {Key: "David", Value: 20},
}

The primary sort criterion is:

Value

and when two values are equal, the secondary criterion is:

Key

Implementation Notes

A Go map should only be used as the input data structure.

The sorted result must not rely on map iteration order.

Instead, the implementation should create an ordered slice containing the key-value pairs and sort that slice according to the required criterion.

The implementation should work with other valid map[string]int inputs and should not depend on the example values.

Scalionix Docs

Keyboard Shortcuts

Navigate the documentation without leaving the keyboard.
Navigation
Previous subject
←
Next subject
→
Previous subsection
Alt + ↑
Next subsection
Alt + ↓
Interface
Documentation Home
Ctrl + Enter
Search
Alt + Q
Open shortcuts
?
Close dialog
Esc
Scalionix Docs

Search Documentation