Sorting Copies
Lesson 39 of 48 in Coddy's GDScript for C Developers course.
C qsort mutates an array and calls a comparator returning a negative, zero or positive int. GDScript sort_custom instead expects a Boolean answering whether the first element should precede the second. Copy the array first when caller storage must remain unchanged. Avoid subtraction-based C comparisons when values could overflow; compare relationally for a three-way result. Keep ordering strict and consistent in both languages. This example sorts small positive integers by last digit, while the task requires lexical word ordering.
Complete C comparison. Save the listed files together and compile the .c files; main is the entry point.
C file main.c:
#include <stdio.h>
#include <stdlib.h>
int compare(const void *a,const void *b) { int x=*(const int *)a%10,y=*(const int *)b%10; return (x>y)-(x<y); }
int main(void) { int items[]={24,11,32}; qsort(items,3,sizeof items[0],compare); printf("%d,%d,%d\n",items[0],items[1],items[2]); return 0; }
C output:
11,32,24GDScript example file main.gd:
extends Node
func _ready() -> void:
var items: Array = [24,11,32]
items.sort_custom(func(a, b) -> bool: return int(a)%10 < int(b)%10)
print("%d,%d,%d" % items)GDScript output:
11,32,24Challenge
EasyImplement SortWords.ordered(words:Array)->Array returning an independent lexicographically sorted outer copy. Words are lowercase ASCII. Solve joins with commas or returns none. Locked probes ensure caller storage is unchanged.
Input is one JSON array on stdin. The locked main parses it, calls the public APIs and prints result= followed by the returned text and a newline. No helper should print. Independent fixed probes verify required API behavior before the input case; they do not enforce implementation style.
Your code goes in sort_words.gd, exercise.gd. main.gd holds the supplied input/output code and cannot be edited. Keep each class_name type in its named file.
Try it yourself
extends Node
func contract_ok(data: Array) -> bool:
var words: Array = ["z","a"]
var result: Array = SortWords.ordered(words)
if result != ["a","z"] or words != ["z","a"]: return false
result.append("x")
if words.size() != 2: return false
return true
func _ready() -> void:
var data: Array = JSON.parse_string(OS.read_string_from_stdin())
if not contract_ok(data):
print("API_CONTRACT_FAILED")
return
print("result=" + Exercise.solve(data))
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in GDScript for C Developers
1Scripts and Scalar Values
Run Script FilesText and CommentsBindings and TypesInteger ArithmeticBoolean ResultsRecap: Stock Label2Flow and Functions
Branches and ValuesPattern MatchingLoops and RangesTyped FunctionsDefault ArgumentsRecap: Command Count5Objects and State
Scripts as ClassesInitializationInstance MethodsRecap: Independent BasketsProperty Setters8Boundaries and Lifetimes
Named Integer StatesNull and Type TestsSorting CopiesReference LifetimesRecap: Sorted TotalsRecap: Ordered CommandsPractice on your own: Online GDScript compiler