Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions homework/length-sort/build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
RMDIR build /S /Q
mkdir build
cd build
cmake -G "MinGW Makefiles" ..
MinGW32-make

lengthSort-ut.exe

cd ..

19 changes: 19 additions & 0 deletions homework/length-sort/sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "sort.hpp"
#include <algorithm>

std::deque<std::string> lengthSort(const std::forward_list<std::string>& words) {
std::deque<std::string> sorted(words.begin(), words.end());

std::sort(sorted.begin(), sorted.end(), [](const std::string& s1, const std::string& s2) {
if (s1.length() == s2.length()) {
for (size_t i = 0; i < s1.length(); i++) {
if (s1[i] != s2[i]) {
return s1[i] < s2[i];
}
}
} else {
return s1.length() < s2.length();
}
});
return sorted;
}
7 changes: 7 additions & 0 deletions homework/length-sort/sort.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <deque>
#include <forward_list>
#include <string>

std::deque<std::string> lengthSort(const std::forward_list<std::string>& words);