C Program
#include <stdio.h> #include <stdlib.h> #include <string.h> char *lines[100], buf[100]; int n = 0; int cmp(const void *a, const void *b) { return strcmp(*(char **)a, *(char **)b); } int main() { FILE *f = fopen("data.txt", "r"); while (fgets(buf, sizeof(buf), f)) lines[n++] = strdup(buf); fclose(f); qsort(lines, n, sizeof(char *), cmp); f = fopen("data.txt", "w"); for (int i = 0; i < n; i++) fputs(lines[i], f), free(lines[i]); fclose(f); }
C Output
Input: Banana Apple Cherry Mango Output: Apple Banana Cherry Mango
C++ Program
#include <fstream> #include <vector> #include <algorithm> using namespace std; int main() { ifstream in("data.txt"); vector<string> v; string s; while (getline(in, s)) v.push_back(s); sort(v.begin(), v.end()); ofstream out("data.txt"); for (auto &x : v) out << x << '\n'; }
C++ Output
Input: Banana Apple Cherry Mango Output: Apple Banana Cherry Mango
JAVA Program
import java.nio.file.*; import java.util.*; public class Main { public static void main(String[] a) throws Exception { List<String> l = Files.readAllLines(Path.of("data.txt")); Collections.sort(l); Files.write(Path.of("data.txt"), l); } }
JAVA Output
Input: Banana Apple Cherry Mango Output: Apple Banana Cherry Mango
Python Program
lines = sorted(open("data.txt").readlines()) open("data.txt", "w").writelines(lines)
Python Output
Input: Banana Apple Cherry Mango Output: Apple Banana Cherry Mango
In-Depth Explanation
Example
Suppose you have a text file with the following list of items:
Banana, Apple, Cherry, Mango.
If you were to sort them in alphabetical order, you would have to read them from the file, sort them, and write them back as a sorted list. The programs below do this in a neat and efficient manner.
Real-Life Analogy
Think of this like organizing a physical list of names on sticky notes stuck randomly on a wall. You remove all the notes, sort them alphabetically, and paste them back in order. This sorting logic is the same in software—just done with file content instead of sticky notes.
Why It Matters
Sorting data is amongst the most basic operations in software development and computer science. Whether names, records, or numbers, sorted data enhances readability, search speeds, and usability. Sorting from a file is typical while processing externally stored data such as logs, datasets, or user inputs.
What It Teaches
This exercise covers file input/output, reading and writing a line, dynamic storage of data (such as arrays or lists), and applying sorting algorithms or built-in functions like sort() in Python or Collections.sort() in Java. It also enforces data manipulation logic, memory management (as in C using strdup), and safe overwriting of files.
Application in Interviews or Actual Projects
Interviewers might want you to generate scripts that sort or tidy up logs, CSVs, or name lists. In actual usage, developers sort all from leaderboard rankings in video games, customer names in admin panels, product listings in shopping websites, to student marksheets and attendance records. Sorting is ubiquitous—from data processing to system scripting—and this file-based one is an excellent exercise.
Sort data from a file by using code is a fundamental programming skill that translates to myriad real-world applications, from text processing to log file analysis and report generation. Be it gearing up for interviews or coding automation scripts, learning this operation enables you to effectively manage and sort data stored in external files. With simple examples in Python, Java, C++, and C, starters can now tackle file sorting issues with confidence and gain real-world skills applicable to data-driven projects, backend systems, and competitive programming.
Social Plugin