Recap - Lambda Sort
Part of the Logic & Flow section of Coddy's GDScript journey. Lesson 56 of 78.
Challenge
EasyWrite 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
1Strings In Depth
String MethodsSearching in StringsReplace and TrimSplit and JoinRecap - Username Check4Dictionaries Part 2
Nested DictionariesArrays of DictionariesMerging DictionariesCopying DictionariesRecap - Frequency Counter10Advanced Functions
Returning Multiple ValuesLambda Functions Part 1Lambda Functions Part 2Recap - Lambda SortRecursive Functions Part 1Recursive Functions Part 2Recap - Sum Nested Array2Advanced Decision Making
Match With VariablesMatch GuardsArray PatternsDictionary PatternsMembership ChecksRecap - Command Parser5Contact Book Application
Display MenuAdd Contact8Student Records Manager
Project OverviewAdd Student3Multi-dimensional Arrays
2D Arrays BasicsAccessing 2D Array ElementsNested Loops with 2D ArraysRecap - 2D ArraysJagged ArraysCommon 2D Array PatternsRecap - Game Board6Sets with Dictionaries
What is a Set?Dictionary as a SetAdding and RemovingUnion and IntersectionSet DifferenceRecap - Remove Duplicates9Advanced Data Aggregation
Summing ValuesFinding Minimum and MaximumCustom SortingRecap - Dictionary SorterPractice on your own: Online GDScript compiler