Menu
CoddyTech

Recap - Lambda Sort

Part of the Logic & Flow section of Coddy's GDScript journey. Lesson 56 of 78.

challenge icon

Challenge

Easy

Write a function named sort_pairs that takes an array data. Each element of data is a pair: an array with a name (a String) and a number, like ["Alice", 25].

The function sorts data by the number, smallest first, and returns it. When two pairs have the same number, the name that comes first alphabetically goes first.

Sort with sort_custom() and a lambda. The lambda receives two pairs, a and b, and returns true when a must come before b. sort_custom() changes the array itself and returns nothing.

For example, sort_pairs([["Alice", 25], ["Bob", 30], ["Charlie", 20]]) returns:

[["Charlie", 20], ["Alice", 25], ["Bob", 30]]

The tests call sort_pairs() themselves. Do not call it, and do not print anything.

Try it yourself

func sort_pairs(data: Array):
	# Write code here
	pass

All lessons in Logic & Flow

Practice on your own: Online GDScript compiler