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