From 671dee9afa2e65b8887c50f74311b3782516083d Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 26 Apr 2019 00:53:57 +0200 Subject: Userland: Add a simple /bin/sort program. --- Userland/sort.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Userland/sort.cpp (limited to 'Userland') diff --git a/Userland/sort.cpp b/Userland/sort.cpp new file mode 100644 index 0000000000..35d3933f9e --- /dev/null +++ b/Userland/sort.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include + +int main(int argc, char** argv) +{ + Vector lines; + + for (;;) { + char buffer[BUFSIZ]; + auto* str = fgets(buffer, sizeof(buffer), stdin); + if (!str) + break; + lines.append(buffer); + } + + quick_sort(lines.begin(), lines.end(), [] (auto& a, auto& b) { + return strcmp(a.characters(), b.characters()) < 0; + }); + + for (auto& line : lines) { + fputs(line.characters(), stdout); + } + + return 0; +} -- cgit v1.2.3