From 863063ceab14ee4ce6787f2bb7c5251a9bb38908 Mon Sep 17 00:00:00 2001 From: Pulsarnixx Date: Sun, 25 Oct 2026 17:42:13 +0100 Subject: [PATCH] Length sort done. --- homework/length-sort/sort.cpp | 22 ++++++++++++++++++++++ homework/length-sort/sort.hpp | 10 ++++++++++ 2 files changed, 32 insertions(+) create mode 100644 homework/length-sort/sort.cpp create mode 100644 homework/length-sort/sort.hpp diff --git a/homework/length-sort/sort.cpp b/homework/length-sort/sort.cpp new file mode 100644 index 000000000..21d7c18aa --- /dev/null +++ b/homework/length-sort/sort.cpp @@ -0,0 +1,22 @@ +#include "sort.hpp" + +bool cmp(const std::string& a, const std::string& b) { + if (a.length() == b.length()) { + return a < b; + } + + return a.length() < b.length(); +} +std::deque lengthSort(std::forward_list collection) { + collection.sort(cmp); + + std::deque dq; + + while (!collection.empty()) { + dq.push_back(collection.front()); + + collection.pop_front(); + } + + return dq; +} diff --git a/homework/length-sort/sort.hpp b/homework/length-sort/sort.hpp new file mode 100644 index 000000000..f6b0a523a --- /dev/null +++ b/homework/length-sort/sort.hpp @@ -0,0 +1,10 @@ +#ifndef SORT_HPP +#define SORT_HPP + +#include +#include // C++11 +#include + +std::deque lengthSort(std::forward_list collection); + +#endif