瀏覽代碼

Userland: Add a simple /bin/sort program.

Andreas Kling 6 年之前
父節點
當前提交
671dee9afa
共有 1 個文件被更改,包括 28 次插入0 次删除
  1. 28 0
      Userland/sort.cpp

+ 28 - 0
Userland/sort.cpp

@@ -0,0 +1,28 @@
+#include <AK/QuickSort.h>
+#include <AK/Vector.h>
+#include <AK/AKString.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(int argc, char** argv)
+{
+    Vector<String> 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;
+}