All templates
Text ManipulationIntermediate
Sort Lines of a Text File
Robomotion•Updated 6 months ago

Overview
Reads a text file, sorts its lines alphabetically, and writes the result back out. Reusable for cleaning up exports and datasets.
Sort Lines of a Text File
Although manipulating text files is an uncomplicated operation, editing files with numerous entries can require a considerable amount of time. Automating processes such as sorting text entries can disengage desktop users from time-demanding monotonous tasks.
What Sort Lines of a Text File can do
- Build Default Path (
Core.Programming.Function) — setsmsg.fixture_pathto$Home$/templates/text-manipulation/sort-lines-of-text-file/fixtures/unsorted.txt. - Intro Dialog (
Core.Dialog.MessageBox,info) titledDescriptionexplaining the flow. - Input Dialog titled
Please select a text file to sort..., defaultmsg.fixture_path→msg.selected_text_file. - Branch On Selection (
Core.Programming.Function,outputs: 2) — requires a non-empty path ending in.txt, otherwise skips to the completion dialog. - Read File (
Core.FileSystem.ReadFile,optBase64: false) →msg.file_contents_raw. - Sort Lines (
Core.Programming.Function) — splits on\n, drops a trailing empty line, sorts withlocaleCompare, rejoins with a trailing newline →msg.sorted_text. - Build Sorted Path (
Core.Programming.Function) — derivesmsg.sorted_file_pathas<dir>\<stem>_Sorted<ext>next to the input. - Write Sorted File (
Core.FileSystem.WriteFile,optMode: truncate,optBase64: false) atmsg.sorted_file_path. - Results Dialog (
Core.Dialog.MessageBox,info, titleFlow completed!) showing both paths, followed by a finalExample completed!dialog andCore.Flow.Stop.
Behind the scenes
- Sorting uses
localeCompareso ordering is locale-aware rather than naive code-point order. Sort Linespops a single trailing empty line before sorting and re-adds one\nat the end — this preserves the common POSIX convention without injecting a blank line into the sorted output.Build Sorted Pathalways joins with a backslash, matching Windows-style output even when the input path uses forward slashes.- Files are read and written with
optBase64: false, so the content round-trips as UTF-8 text.