{"id":297939,"date":"2020-01-30T09:00:26","date_gmt":"2020-01-30T09:00:26","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=297939"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2020-01-30T09:00:26","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=297939","title":{"rendered":"\u041a\u0430\u043a \u0436\u0435, \u0447\u0435\u0440\u0442 \u0432\u043e\u0437\u044c\u043c\u0438, \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0441\u043f\u0438\u0441\u043e\u043a?"},"content":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/485882\/\">\u041f\u0443\u0441\u0442\u044c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u2014 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u044b \u043a\u043b\u0430\u0441\u0441\u043e\u0432 File \u0438 Folder. \u0417\u0430\u0434\u0430\u0447\u0430 \u2014 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0441\u043f\u0438\u0441\u043e\u043a. \u0415\u0441\u0442\u044c \u0434\u0432\u0430 \u0448\u0438\u0440\u043e\u043a\u043e \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0430 \u2014 \u043a\u043e\u0433\u0434\u0430 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u0434\u0443\u0442 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0438, \u0430 \u043f\u043e\u0442\u043e\u043c \u0444\u0430\u0439\u043b\u044b, \u0438 \u043a\u043e\u0433\u0434\u0430 \u0444\u0430\u0439\u043b\u044b \u0438 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0432\u043f\u0435\u0440\u0435\u043c\u0435\u0448\u043a\u0443. \u041d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0434\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043b\u044e\u0431\u043e\u0439 \u0438\u0437 \u044d\u0442\u0438\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432.<\/p>\n<p>  \u0412\u043f\u0435\u0440\u0432\u044b\u0435 \u043d\u0430 \u043f\u043e\u0445\u043e\u0436\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u044f \u043d\u0430\u0442\u043a\u043d\u0443\u043b\u0441\u044f \u043d\u0430 \u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u0435. \u041a \u0441\u043b\u043e\u0432\u0443, \u0440\u0435\u0448\u0435\u043d\u0430 \u044d\u0442\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0431\u044b\u043b\u0430 \u043c\u0435\u0442\u043e\u0434\u043e\u043c 3 \u0438\u0437 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438. \u00ab\u041a\u0430\u043a \u043d\u0435\u044d\u0441\u0442\u0435\u0442\u0438\u0447\u043d\u043e\u00bb, \u2014 \u043f\u043e\u0434\u0443\u043c\u0430\u043b \u044f. \u2014 \u00ab\u0414\u0430\u043b\u0438 \u0431\u044b \u043c\u043d\u0435 \u044d\u0442\u0443 \u0437\u0430\u0434\u0430\u0447\u0443, \u044f \u0431\u044b \u0442\u043e\u0447\u043d\u043e \u0440\u0435\u0448\u0438\u043b \u0435\u0435 \u0432 128 \u0440\u0430\u0437 \u043b\u0443\u0447\u0448\u0435. \u041d\u043e\u2026 \u043a\u0430\u043a?\u00bb<\/p>\n<p>  \u0421 \u0442\u043e\u0439 \u043f\u043e\u0440\u044b \u043c\u0438\u043d\u0443\u043b\u043e \u0443\u0436\u0435 5 \u043b\u0435\u0442, \u0430 \u044f, \u0432\u0440\u0435\u043c\u044f \u043e\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043c\u044b\u0441\u043b\u0435\u043d\u043d\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044f\u0441\u044c \u043a \u0434\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435, \u0442\u0430\u043a \u0438 \u043d\u0435 \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u043b \u043a \u043d\u0435\u0439 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u0437\u0430 \u044d\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u0443 \u043c\u0435\u043d\u044f \u0443\u0441\u043f\u0435\u043b\u043e \u043d\u0430\u043a\u043e\u043f\u0438\u0442\u044c\u0441\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0438\u0434\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u044f \u0445\u043e\u0447\u0443 \u0441 \u0432\u0430\u043c\u0438 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h3>\u041c\u0435\u0442\u043e\u0434 1. \u0427\u0438\u0441\u043b\u043e\u0432\u043e\u0439 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430<\/h3>\n<p>  \u041d\u0430\u0432\u0435\u0440\u043d\u043e, \u0441\u0430\u043c\u0430\u044f \u043f\u0435\u0440\u0432\u0430\u044f \u0438\u0434\u0435\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443. \u041f\u0443\u0441\u0442\u044c \u0443 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043f\u043e\u043c\u0438\u043c\u043e \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 getName() \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d \u043c\u0435\u0442\u043e\u0434 int getPriority(), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435. \u0412 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u0430\u0445 \u0431\u0443\u0434\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0442\u0438\u043f\u0430 int, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0438 \u0431\u0443\u0434\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043d\u0443\u0436\u043d\u044b\u0439 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442. \u0412\u043f\u0440\u043e\u0447\u0435\u043c, \u0432 \u043a\u043e\u0434\u0435 \u0432\u0441\u0435 \u044d\u0442\u043e \u043f\u0440\u043e\u0449\u0435, \u0447\u0435\u043c \u044f \u043f\u044b\u0442\u0430\u044e\u0441\u044c \u043e\u0431\u044a\u044f\u0441\u043d\u0438\u0442\u044c:  <\/p>\n<pre><code class=\"cpp\">class Base { public:      virtual int getPriority() const = 0;      virtual const std::string&amp; getName() const = 0;      virtual const std::string getPrintedName() const = 0;      virtual ~Base() = default;  };  class File: public Base { public:      static const int PRIORITY;  public:      File(const std::string &amp;name)         : name(name)     {}      const std::string&amp; getName() const override {         return name;     }      const std::string getPrintedName() const override {         return &quot;File_&quot; + getName();     }      int getPriority() const override {         return PRIORITY;     }  private:      std::string name; };  const int File::PRIORITY = 1;  class Folder: public Base { public:      static const int PRIORITY;  public:      Folder(const std::string &amp;name)         : name(name)     {}      const std::string&amp; getName() const override {         return name;     }      const std::string getPrintedName() const override {         return &quot;Folder_&quot; + getName();     }      int getPriority() const override {         return PRIORITY;     }  private:      std::string name; };  const int Folder::PRIORITY = 2; <\/code><\/pre>\n<p>  \u041a\u043e\u043c\u043f\u0430\u0440\u0430\u0442\u043e\u0440 \u0434\u043b\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438:  <\/p>\n<pre><code class=\"cpp\">bool comparator(const std::unique_ptr&lt;Base&gt; &amp;first, const std::unique_ptr&lt;Base&gt; &amp;second) {     const int priorityFirst = first-&gt;getPriority();     const int prioritySecond = second-&gt;getPriority();     if (priorityFirst != prioritySecond) {         return priorityFirst &gt; prioritySecond;     } else {         return first-&gt;getName() &lt; second-&gt;getName();     } } <\/code><\/pre>\n<p>  \u0417\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432 \u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c:  <\/p>\n<pre><code class=\"cpp\">    std::vector&lt;std::unique_ptr&lt;Base&gt;&gt; files;     files.emplace_back(std::make_unique&lt;Folder&gt;(&quot;1&quot;));     files.emplace_back(std::make_unique&lt;File&gt;(&quot;3&quot;));     files.emplace_back(std::make_unique&lt;File&gt;(&quot;2&quot;));     files.emplace_back(std::make_unique&lt;Folder&gt;(&quot;4&quot;));      std::sort(files.begin(), files.end(), comparator);      for (const std::unique_ptr&lt;Base&gt; &amp;element: files) {         std::cout &lt;&lt; element-&gt;getPrintedName() &lt;&lt; &quot; &quot;;     }              std::cout &lt;&lt; std::endl; <\/code><\/pre>\n<p>  <a href=\"https:\/\/github.com\/magisterRab\/sort_files_test\/blob\/master\/sort_priority\/main.cpp\" rel=\"nofollow\">\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434<\/a><\/p>\n<p>  \u0412\u0441\u0435 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0438 \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0441\u0432\u0435\u0440\u0445\u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0435\u0439 \u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u043f\u0438\u0441\u0430\u0442\u044c \u0438 \u0447\u0438\u0442\u0430\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f. \u041d\u043e \u0435\u0441\u0442\u044c \u0443 \u044d\u0442\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u043c\u0438\u043d\u0443\u0441\u044b:<br \/>  1) \u0422\u0438\u043f \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u0430 \u2014 int. \u0412 \u043a\u0430\u043a\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u043b\u0435\u0436\u0430\u0442\u044c \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c\u044b\u0435 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u044b \u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u043e\u0432? \u042d\u0442\u0438 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u044b, \u043e\u043d\u0438 \u043d\u0443\u043c\u0435\u0440\u0443\u044e\u0442\u0441\u044f \u0441 0, \u0441 1, \u043c\u043e\u0433\u0443\u0442 \u043b\u0438 \u043e\u043d\u0438 \u0431\u044b\u0442\u044c \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438?<br \/>  2) \u0415\u0441\u043b\u0438 \u043c\u044b \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u0435\u043c \u0432\u0432\u0435\u0441\u0442\u0438 \u043d\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 (\u043a\u0430\u043a\u043e\u0439-\u043d\u0438\u0431\u0443\u0434\u044c Symlink \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440) \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0432 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435 \u043e\u043d \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u043b\u0441\u044f \u043c\u0435\u0436\u0434\u0443 \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c\u0438 \u043a\u043b\u0430\u0441\u0441\u0430\u043c\u0438, \u0442\u043e \u043d\u0430\u043c \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u00ab\u0441\u043c\u0435\u0449\u0430\u0442\u044c\u00bb \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442. \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u2014 \u043a\u0430\u043a \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0432\u0441\u0435\u0445 \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u0430 Base?<br \/>  3) \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u043e\u0431\u044f\u0437\u0430\u043d\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u0430\u043c\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432. \u0421\u0430\u043c\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0444\u0430\u0439\u043b\u0430, \u043f\u0430\u043f\u043a\u0438 \u0432\u043f\u043e\u043b\u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u0438 \u043d\u0435 \u043f\u043e\u0434\u043e\u0437\u0440\u0435\u0432\u0430\u0442\u044c, \u0447\u0442\u043e \u0438\u0445 \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0442\u0441\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c.<\/p>\n<p>  \u0412 \u043e\u0431\u0449\u0435\u043c, \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u043d\u0430\u043c \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442, \u0434\u0432\u0438\u0433\u0430\u0435\u043c\u0441\u044f \u0434\u0430\u043b\u044c\u0448\u0435<\/p>\n<h3>\u041c\u0435\u0442\u043e\u0434 2. \u0418\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u043a\u043b\u0430\u0441\u0441\u0430 \u043f\u043e \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u043c\u0443 \u0442\u0438\u043f\u0443 \u043a\u043b\u0430\u0441\u0441\u0430<\/h3>\n<p>  \u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u043c\u043e\u0436\u043d\u043e \u0432\u0432\u0435\u0441\u0442\u0438 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0449\u0443\u044e \u0442\u0438\u043f \u043a\u043b\u0430\u0441\u0441\u0430.  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043a\u043b\u0430\u0441\u0441\u043e\u0432<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">class Base { public:      virtual std::string getType() const = 0;      virtual const std::string&amp; getName() const = 0;      virtual const std::string getPrintedName() const = 0;      virtual ~Base() = default;  };  class File: public Base { public:      static const std::string TYPE;  public:      File(const std::string &amp;name)         : name(name)     {}      const std::string&amp; getName() const override {         return name;     }      const std::string getPrintedName() const override {         return &quot;File_&quot; + getName();     }      std::string getType() const override {         return TYPE;     }  private:      std::string name; };  const std::string File::TYPE = &quot;File&quot;;  class Folder: public Base { public:      static const std::string TYPE;  public:      Folder(const std::string &amp;name)         : name(name)     {}      const std::string&amp; getName() const override {         return name;     }      const std::string getPrintedName() const override {         return &quot;Folder_&quot; + getName();     }      std::string getType() const override {         return TYPE;     }  private:      std::string name; };  const std::string Folder::TYPE = &quot;Folder&quot;; <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u041a\u043e\u043c\u043f\u0430\u0440\u0430\u0442\u043e\u0440:  <\/p>\n<pre><code class=\"cpp\">bool comparator(const std::unique_ptr&lt;Base&gt; &amp;first, const std::unique_ptr&lt;Base&gt; &amp;second) {     if (first-&gt;getType() == Folder::TYPE &amp;&amp; second-&gt;getType() == Folder::TYPE) {         return first-&gt;getName() &lt; second-&gt;getName();     } else if (first-&gt;getType() == Folder::TYPE &amp;&amp; second-&gt;getType() == File::TYPE) {         return true;     } else if (first-&gt;getType() == File::TYPE &amp;&amp; second-&gt;getType() == Folder::TYPE) {         return false;     } else {         return first-&gt;getName() &lt; second-&gt;getName();     } } <\/code><\/pre>\n<p>  \u0417\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043d\u0438\u0447\u0435\u043c \u043d\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u043e\u0442 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0417\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">    std::vector&lt;std::unique_ptr&lt;Base&gt;&gt; files;     files.emplace_back(std::make_unique&lt;Folder&gt;(&quot;1&quot;));     files.emplace_back(std::make_unique&lt;File&gt;(&quot;3&quot;));     files.emplace_back(std::make_unique&lt;File&gt;(&quot;2&quot;));     files.emplace_back(std::make_unique&lt;Folder&gt;(&quot;4&quot;));      std::sort(files.begin(), files.end(), comparator);      for (const std::unique_ptr&lt;Base&gt; &amp;element: files) {         std::cout &lt;&lt; element-&gt;getPrintedName() &lt;&lt; &quot; &quot;;     } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <a href=\"https:\/\/github.com\/magisterRab\/sort_files_test\/blob\/master\/sort_name\/main.cpp\" rel=\"nofollow\">\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434<\/a><\/p>\n<p>  \u0412 \u043e\u0431\u0449\u0435\u043c, \u0442\u043e\u0436\u0435 \u043e\u0441\u043e\u0431\u043e \u043d\u0438\u0447\u0435\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0433\u043e. \u041d\u043e \u0442\u0430\u043a\u0436\u0435 \u0435\u0441\u0442\u044c \u0438 \u043c\u0438\u043d\u0443\u0441\u044b.<br \/>  \u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u044f\u0432\u043d\u043e \u0441\u0442\u0430\u043b\u0430 \u0441\u043b\u043e\u0436\u043d\u0435\u0435. \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435, \u0447\u0442\u043e \u0431\u0443\u0434\u0435\u0442, \u0435\u0441\u043b\u0438 \u0441\u044e\u0434\u0430 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 (\u0442\u043e\u0442 \u0436\u0435 Symlink).<br \/>  \u0412\u043e-\u0432\u0442\u043e\u0440\u044b\u0445, \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u044f \u043d\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u043f\u0443\u0442\u0435\u043c \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e, \u043c\u043e\u0436\u043d\u043e \u043d\u0435\u0447\u0430\u044f\u043d\u043d\u043e \u0437\u0430\u0431\u044b\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u044b\u0439 \u0442\u0438\u043f \u043a\u043b\u0430\u0441\u0441\u0430, \u0447\u0442\u043e \u043f\u043e\u0432\u043b\u0435\u0447\u0435\u0442 \u0437\u0430 \u0441\u043e\u0431\u043e\u0439 \u0432\u0435\u0441\u0435\u043b\u0443\u044e, \u043d\u043e \u043c\u0443\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u043e\u0442\u043b\u0430\u0434\u043a\u0443.<\/p>\n<p>  \u0412 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u0438\u0437 \u044d\u0442\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u043f\u043e\u0447\u0442\u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u043b\u0435\u0434\u0443\u0435\u0442<\/p>\n<h3>\u041c\u0435\u0442\u043e\u0434 3. \u0418\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u043a\u043b\u0430\u0441\u0441\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e rtti<\/h3>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f (\u0441\u043a\u0443\u0434\u043d\u044b\u043c\u0438 \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442) \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044f\u043c\u0438 C++ \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0440\u0435\u0444\u043b\u0435\u043a\u0441\u0438\u0438 \u0438 \u0438\u0437\u0431\u0430\u0432\u0438\u043c\u0441\u044f \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043e\u0442 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0449\u0435\u0439 \u0442\u0438\u043f \u043a\u043b\u0430\u0441\u0441\u0430. \u0412\u0435\u0434\u044c \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0432 \u0442\u0438\u043f \u043a\u043b\u0430\u0441\u0441\u0430 \u0432 runtime \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 rtti.<\/p>\n<p>  \u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0441\u0442\u0430\u043b\u0430 \u043f\u0440\u043e\u0449\u0435:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043a\u043b\u0430\u0441\u0441\u043e\u0432<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">class Base { public:      virtual const std::string&amp; getName() const = 0;      virtual const std::string getPrintedName() const = 0;      virtual ~Base() = default;  };  class File: public Base { public:      File(const std::string &amp;name)         : name(name)     {}      const std::string&amp; getName() const override {         return name;     }      const std::string getPrintedName() const override {         return &quot;File_&quot; + getName();     }  private:      std::string name; };  class Folder: public Base { public:      Folder(const std::string &amp;name)         : name(name)     {}      const std::string&amp; getName() const override {         return name;     }      const std::string getPrintedName() const override {         return &quot;Folder_&quot; + getName();     }  private:      std::string name; }; <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0422\u0443\u0442 \u0443\u0436\u0435 \u043d\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u043b\u0438\u0448\u043d\u0438\u0445 \u043f\u043e\u043b\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u044b \u0441\u0430\u043c\u043e\u043c\u0443 \u043a\u043b\u0430\u0441\u0441\u0443, \u043d\u043e \u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0443 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438.<\/p>\n<p>  \u041f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u0441\u0430\u043c\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0438\u0441\u044c.<\/p>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043a\u043e\u043c\u043f\u0430\u0440\u0430\u0442\u043e\u0440:  <\/p>\n<pre><code class=\"cpp\">bool comparator(const std::unique_ptr&lt;Base&gt; &amp;first, const std::unique_ptr&lt;Base&gt; &amp;second) {     if (dynamic_cast&lt;Folder*&gt;(first.get()) != nullptr &amp;&amp; dynamic_cast&lt;Folder*&gt;(second.get()) != nullptr) {         return first-&gt;getName() &lt; second-&gt;getName();     } else if (dynamic_cast&lt;Folder*&gt;(first.get()) != nullptr &amp;&amp; dynamic_cast&lt;File*&gt;(second.get()) != nullptr) {         return true;     } else if (dynamic_cast&lt;File*&gt;(first.get()) != nullptr &amp;&amp; dynamic_cast&lt;Folder*&gt;(second.get()) != nullptr) {         return false;     } else {         return first-&gt;getName() &lt; second-&gt;getName();     } } <\/code><\/pre>\n<p>  \u0413\u043e\u0441\u043f\u043e\u0434\u0438\u2026<\/p>\n<p>  \u0414\u0430, \u043d\u0430\u0432\u0435\u0440\u043d\u043e \u044d\u0442\u043e\u0442 \u043a\u0443\u0441\u043e\u043a \u043a\u043e\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u043a\u0430\u043a-\u0442\u043e \u043a\u0440\u0430\u0441\u0438\u0432\u043e \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c, \u043d\u043e\u2026<br \/>  \u0415\u0449\u0435 \u043e\u0434\u0438\u043d \u043c\u0438\u043d\u0443\u0441 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u043a\u043b\u044e\u0447\u0430\u0442\u044c rtti \u043f\u0440\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b (\u043a\u043e\u0442\u043e\u0440\u044b\u0439, \u043a \u043c\u043e\u0435\u043c\u0443 \u0443\u0434\u0438\u0432\u043b\u0435\u043d\u0438\u044e, \u043e\u043a\u0430\u0437\u0430\u043b\u0441\u044f \u0432\u043a\u043b\u044e\u0447\u0435\u043d \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e, \u043f\u043e \u043a\u0440\u0430\u0439\u043d\u0435\u0439 \u043c\u0435\u0440\u0435 \u0432 gcc)<\/p>\n<p>  \u0412 \u043e\u0431\u0449\u0435\u043c, \u0442\u0430\u043a \u0441\u0435\u0431\u0435 \u043c\u0435\u0442\u043e\u0434, \u043a\u0430\u043a \u043f\u043e \u043c\u043d\u0435.<\/p>\n<h3>\u041c\u0435\u0442\u043e\u0434 4. \u041c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u044f<\/h3>\n<p>  \u0413\u043b\u044f\u0434\u044f \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 \u0434\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430, \u0443 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0438\u0437 \u0432\u0430\u0441 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u043f\u043e\u0441\u0435\u043b\u0438\u043b\u0430\u0441\u044c \u043c\u044b\u0441\u043b\u044c, \u0447\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u044f \u043f\u044b\u0442\u0430\u044e\u0441\u044c \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430\u2026<\/p>\n<p>  \u0425\u043e\u0442\u044f \u043a\u043e\u0433\u043e \u044f \u0442\u0443\u0442 \u0438\u043d\u0442\u0440\u0438\u0433\u0443\u044e, \u0432\u044b \u0436\u0435 \u0443\u0436\u0435 \u0432\u0438\u0434\u0435\u043b\u0438 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a.<\/p>\n<p>  \u0412 \u043e\u0431\u0449\u0435\u043c, \u0434\u0430, \u044d\u0442\u043e \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u044e. \u041e\u043d\u0430 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0442\u043e\u0433\u0434\u0430, \u043a\u043e\u0433\u0434\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 (\u0432 \u0441\u043c\u044b\u0441\u043b\u0435 \u041e\u041e\u041f) \u043d\u0435 \u043e\u0442 \u043e\u0434\u043d\u043e\u0433\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430, \u0430 \u0441\u0440\u0430\u0437\u0443 \u043e\u0442 \u0434\u0432\u0443\u0445, \u0442\u0440\u0435\u0445\u2026<br \/>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0433\u0435\u043e\u043c\u0435\u0442\u0440\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0444\u0438\u0433\u0443\u0440: \u043a\u0432\u0430\u0434\u0440\u0430\u0442, \u043a\u0440\u0443\u0433, \u0442\u0440\u0435\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a. \u0418 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043d\u0430\u0439\u0442\u0438 \u043f\u043b\u043e\u0449\u0430\u0434\u044c \u043f\u0435\u0440\u0435\u0441\u0435\u0447\u0435\u043d\u0438\u044f \u043b\u044e\u0431\u044b\u0445 \u0434\u0432\u0443\u0445 \u0438\u0437 \u044d\u0442\u0438\u0445 \u0444\u0438\u0433\u0443\u0440. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c, \u043d\u0430\u043c \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b \u0444\u043e\u0440\u043c\u0443\u043b\u044b \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043b\u043e\u0449\u0430\u0434\u0438 \u043f\u0435\u0440\u0435\u0441\u0435\u0447\u0435\u043d\u0438\u044f \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0430 \u0438 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0430, \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0430 \u0438 \u043a\u0443\u0431\u0430 \u0438 \u0442.\u0434.<\/p>\n<p>  \u0420\u0435\u0448\u0438\u0442\u044c \u044d\u0442\u0443 \u0437\u0430\u0434\u0430\u0447\u0443 \u043c\u043e\u0436\u043d\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0448\u0430\u0431\u043b\u043e\u043d\u0430 Visitor, \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0432\u0448\u0438\u0441\u044c \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e C++-\u043d\u044b\u043c\u0438 variant-\u0430\u043c\u0438.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043a\u043b\u0430\u0441\u0441\u043e\u0432<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">class File { public:      File(const std::string &amp;name)         : name(name)     {}      const std::string&amp; getName() const {         return name;     }      const std::string getPrintedName() const {         return &quot;File_&quot; + getName();     }  private:      std::string name; };  class Folder { public:      Folder(const std::string &amp;name)         : name(name)     {}      const std::string&amp; getName() const {         return name;     }      const std::string getPrintedName() const {         return &quot;Folder_&quot; + getName();     }  private:      std::string name; };  class Symlink { public:      Symlink(const std::string &amp;name)         : name(name)     {}      const std::string&amp; getName() const {         return name;     }      const std::string getPrintedName() const {         return &quot;Symlink_&quot; + getName();     }  private:      std::string name; }; <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0412 \u043a\u043b\u0430\u0441\u0441\u0430\u0445 \u043d\u0435\u0442 \u043d\u0438\u0447\u0435\u0433\u043e \u043b\u0438\u0448\u043d\u0435\u0433\u043e. \u041e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0434\u0430\u0436\u0435 \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441, \u0442\u0430\u043a \u043a\u0430\u043a \u043c\u044b \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f variant-\u043e\u043c. \u0412\u0437\u0430\u043c\u0435\u043d \u044f \u043f\u0440\u0438\u043d\u0435\u0441 \u043d\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 Symlink, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0442\u0430\u043a \u0434\u043e\u043b\u0433\u043e \u0433\u0440\u043e\u0437\u0438\u043b\u0441\u044f \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c.<\/p>\n<p>  \u0412\u0432\u0435\u0434\u0435\u043c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0442\u0438\u043f, \u044f\u0432\u043b\u044f\u044e\u0449\u0438\u0439\u0441\u044f Variant-\u043e\u043c \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430:  <\/p>\n<pre><code class=\"cpp\">using Element = std::variant&lt;Folder, File, Symlink&gt;; <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0438 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b:  <\/p>\n<pre><code class=\"cpp\">    std::vector&lt;Element&gt; files = {Folder(&quot;1&quot;), File(&quot;3&quot;), File(&quot;2&quot;), Folder(&quot;4&quot;), Symlink(&quot;1&quot;), Symlink(&quot;5&quot;), Symlink(&quot;1&quot;)};      std::sort(files.begin(), files.end(), comparator);      for (const Element &amp;element: files) {         std::visit([](const auto &amp;elem) {             std::cout &lt;&lt; elem.getPrintedName() &lt;&lt; &quot; &quot;;         }, element);     }              std::cout &lt;&lt; std::endl; <\/code><\/pre>\n<p>  \u0412 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0434\u0430\u0436\u0435 \u043f\u0440\u043e\u0449\u0435, \u0447\u0435\u043c \u0432\u043e \u0432\u0441\u0435\u0445 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430\u0445.<\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u2014 \u043a\u043e\u043c\u043f\u0430\u0440\u0430\u0442\u043e\u0440:  <\/p>\n<pre><code class=\"cpp\">bool operator&lt;(const Folder &amp;first, const Folder &amp;second) {     return first.getName() &lt; second.getName(); }  bool operator&lt;(const Folder &amp;first, const File &amp;second) {     return true; }  bool operator&lt;(const Folder &amp;first, const Symlink &amp;second) {     return true; }  bool operator&lt;(const File &amp;first, const Folder &amp;second) {     return false; }  bool operator&lt;(const File &amp;first, const File &amp;second) {     return first.getName() &lt; second.getName(); }  bool operator&lt;(const File &amp;first, const Symlink &amp;second) {     return true; }  bool operator&lt;(const Symlink &amp;first, const Folder &amp;second) {     return false; }  bool operator&lt;(const Symlink &amp;first, const File &amp;second) {     return false; }  bool operator&lt;(const Symlink &amp;first, const Symlink &amp;second) {     return first.getName() &lt; second.getName(); }  bool comparator(const Element &amp;first, const Element &amp;second) {     const bool result = std::visit([&amp;](const auto &amp;contentFirst){         return std::visit([&amp;](const auto &amp;contentSecond){             return contentFirst &lt; contentSecond;         }, second);     }, first);     return result; } <\/code><\/pre>\n<p>  <a href=\"https:\/\/github.com\/magisterRab\/sort_files_test\/blob\/master\/sort_multiple_dispatch\/main.cpp\" rel=\"nofollow\">\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434<\/a><\/p>\n<p>  \u0421\u0430\u043c \u043a\u043e\u043c\u043f\u0430\u0440\u0430\u0442\u043e\u0440 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u2014 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f std::visit \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0434\u043b\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u043f\u043e\u0442\u043e\u043c \u0434\u043b\u044f \u0432\u0442\u043e\u0440\u043e\u0433\u043e. \u041f\u043e\u0442\u043e\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0430 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f. \u0422\u0430\u043a \u043a\u0430\u043a std::visit \u00ab\u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u00bb \u0442\u0438\u043f \u043b\u0435\u0436\u0430\u0449\u0435\u0433\u043e \u0432 variant-\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0442\u043e \u043f\u0440\u0438 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0438 \u0432\u044b\u0431\u0435\u0440\u0435\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0430\u044f \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/p>\n<p>  \u0412\u043e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0430\u043c\u0438 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442\u2026 \u0433\u0440\u043e\u043c\u043e\u0437\u0434\u043a\u043e. \u041d\u0430\u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u0442\u043e \u0438 \u043d\u0438 \u0440\u0430\u0437\u0443 \u043d\u0435 \u043e\u0448\u0438\u0431\u0438\u0442\u044c\u0441\u044f \u2014 \u0437\u0430\u0434\u0430\u0447\u0430 \u0434\u043b\u044f \u0442\u0435\u0440\u043f\u0435\u043b\u0438\u0432\u044b\u0445.<br \/>  \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043c\u043e\u0436\u043d\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0448\u0430\u0431\u043b\u043e\u043d\u043e\u0432 <s>\u0438 \u0447\u044c\u0435\u0439-\u0442\u043e \u043c\u0430\u0442\u0435\u0440\u0438<\/s>   <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0431\u043e\u0439\u043b\u0435\u0440\u043f\u0440\u0438\u043d\u0442\u0430<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">struct ComparePriority {     int operator()(const Folder &amp;first, const File &amp;second) const {         return 1;     }      int operator()(const Folder &amp;first, const Symlink &amp;second) const {         return 1;     }      int operator()(const Symlink &amp;first, const File &amp;second) const {         return -1;     } };  bool comparator(const Element &amp;first, const Element &amp;second) {     const bool result = std::visit([&amp;](const auto &amp;contentFirst){         return std::visit([&amp;](const auto &amp;contentSecond){             using TypeFirst = std::decay_t&lt;decltype(contentFirst)&gt;;             using TypeSecond = std::decay_t&lt;decltype(contentSecond)&gt;;             int compared;             if constexpr (std::is_invocable_r_v&lt;int, ComparePriority, TypeFirst, TypeSecond&gt;) {                 compared = ComparePriority()(contentFirst, contentSecond);             } else if constexpr (std::is_invocable_r_v&lt;int, ComparePriority, TypeSecond, TypeFirst&gt;) {                 compared = -ComparePriority()(contentSecond, contentFirst);             } else if constexpr (std::is_same_v&lt;TypeFirst, TypeSecond&gt;){                 compared = 0;             } else {                 static_assert(std::is_same_v&lt;TypeFirst, TypeFirst&gt;, &quot;Error&quot;);             }             if (compared == 0) {                 return contentFirst.getName() &lt; contentSecond.getName();             } else {                 return compared == 1;             }         }, second);     }, first);     return result; } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <a href=\"https:\/\/github.com\/magisterRab\/sort_files_test\/blob\/master\/sort_multiple_dispatch_2\/main.cpp\" rel=\"nofollow\">\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434<\/a><\/p>\n<p>  \u043d\u043e \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043b\u0443\u0447\u0448\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442. <\/p>\n<h3>\u041c\u0435\u0442\u043e\u0434 5. \u041c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0441 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u043c<\/h3>\n<p>  \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u0439 \u0436\u0435 \u0441\u0430\u043c\u043e\u0439, \u043a\u0430\u043a \u0438 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043a\u043b\u0430\u0441\u0441\u043e\u0432<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">class File { public:          File(const std::string &amp;name)         : name(name)     {}          const std::string&amp; getName() const {         return name;     }      const std::string getPrintedName() const {         return &quot;File_&quot; + getName();     }  private:          std::string name; };  class Folder { public:          Folder(const std::string &amp;name)         : name(name)     {}          const std::string&amp; getName() const {         return name;     }      const std::string getPrintedName() const {         return &quot;Folder_&quot; + getName();     }  private:          std::string name; };  class Symlink { public:      Symlink(const std::string &amp;name)         : name(name)     {}      const std::string&amp; getName() const {         return name;     }      const std::string getPrintedName() const {         return &quot;Symlink_&quot; + getName();     }  private:      std::string name; };  using Element = std::variant&lt;Folder, File, Symlink&gt;; <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u041a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u2014 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u043b\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u0430 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430:  <\/p>\n<pre><code class=\"cpp\">int priority(const Folder &amp;) {     return 3; }  int priority(const File &amp;) {     return 2; }  int priority(const Symlink &amp;) {     return 1; } <\/code><\/pre>\n<p>  \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0441\u0430\u043c \u043a\u043e\u043c\u043f\u0430\u0440\u0430\u0442\u043e\u0440 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0442\u0430\u043a\u0438\u043c:  <\/p>\n<pre><code class=\"cpp\">bool comparator(const Element &amp;first, const Element &amp;second) {     const bool result = std::visit([&amp;](const auto &amp;contentFirst){         return std::visit([&amp;](const auto &amp;contentSecond){             const int priorityFirst = priority(contentFirst);             const int prioritySecond = priority(contentSecond);             if (priorityFirst != prioritySecond) {                 return priorityFirst &gt; prioritySecond;             } else {                 return contentFirst.getName() &lt; contentSecond.getName();             }         }, second);     }, first);     return result; } <\/code><\/pre>\n<p>  <a href=\"https:\/\/github.com\/magisterRab\/sort_files_test\/blob\/master\/sort_multiple_dispatch_with_priority\/main.cpp\" rel=\"nofollow\">\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434<\/a><\/p>\n<p>  \u041e\u043a\u0435\u0439, \u0432\u0441\u0435 \u0445\u043e\u0440\u043e\u0448\u043e. \u041d\u043e \u0435\u0441\u043b\u0438 \u0432\u044b \u043f\u043e\u043c\u043d\u0438\u0442\u0435, \u0432 \u0441\u0430\u043c\u043e\u043c \u043d\u0430\u0447\u0430\u043b\u0435 \u044f \u043e\u0431\u0435\u0449\u0430\u043b \u0434\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u044b \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u043f\u0440\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432. \u0421\u0435\u0439\u0447\u0430\u0441 \u0436\u0435 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u0438 \u0441\u0430\u043c\u044b\u0435 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u044b \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043e \u043d\u0430\u0447\u0430\u043b\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438, \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043e\u0434\u0430. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u0437\u0430\u0434\u0430\u043d\u0438\u0435 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u0432 \u0432 runtime \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u0438\u0437 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f.<\/p>\n<h3>\u041c\u0435\u0442\u043e\u0434 6. \u0427\u0438\u0441\u043b\u043e\u0432\u043e\u0439 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442, \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u0443\u0435\u043c\u044b\u0439 \u0438\u0437\u0432\u043d\u0435<\/h3>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u0441\u043f\u043e\u043c\u043d\u0438\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0438\u0437 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043a\u043b\u0430\u0441\u0441\u043e\u0432<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">class Base { public:      virtual int getPriority() const = 0;      virtual const std::string&amp; getName() const = 0;      virtual const std::string getPrintedName() const = 0;      virtual ~Base() = default;  };  class File: public Base { public:      static int PRIORITY;  public:      File(const std::string &amp;name)         : name(name)     {}      const std::string&amp; getName() const override {         return name;     }      const std::string getPrintedName() const override {         return &quot;File_&quot; + getName();     }      int getPriority() const override {         return PRIORITY;     }  private:      std::string name; };  int File::PRIORITY = 0;  class Folder: public Base { public:      static int PRIORITY;  public:      Folder(const std::string &amp;name)         : name(name)     {}      const std::string&amp; getName() const override {         return name;     }      const std::string getPrintedName() const override {         return &quot;Folder_&quot; + getName();     }      int getPriority() const override {         return PRIORITY;     }  private:      std::string name; };  int Folder::PRIORITY = 0;  bool comparator(const std::unique_ptr&lt;Base&gt; &amp;first, const std::unique_ptr&lt;Base&gt; &amp;second) {     const int priorityFirst = first-&gt;getPriority();     const int prioritySecond = second-&gt;getPriority();     if (priorityFirst != prioritySecond) {         return priorityFirst &gt; prioritySecond;     } else {         return first-&gt;getName() &lt; second-&gt;getName();     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0418 \u043f\u043e\u0434\u0443\u043c\u0430\u0435\u043c, \u043a\u0430\u043a \u0441\u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u044b. \u041d\u0443 \u0442\u0443\u0442 \u0432\u0441\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u2014 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0447\u043b\u0435\u043d\u044b PRIORITY \u0438 \u043e\u0442\u0434\u0430\u0434\u0438\u043c \u0438\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e:  <\/p>\n<pre><code class=\"cpp\">    std::vector&lt;std::pair&lt;std::string, std::reference_wrapper&lt;int&gt;&gt;&gt; priorities{{&quot;Folder&quot;, std::ref(Folder::PRIORITY)}, {&quot;File&quot;, std::ref(File::PRIORITY)}};      for (const auto &amp;[type, ptr]: priorities) {         if (type == &quot;Folder&quot;) {             ptr.get() = 2;         } else if (type == &quot;File&quot;) {             ptr.get() = 1;         }     } <\/code><\/pre>\n<p>  <a href=\"https:\/\/github.com\/magisterRab\/sort_files_test\/blob\/master\/sort_priority_external\/main.cpp\" rel=\"nofollow\">\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434<\/a><\/p>\n<p>  \u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0443\u0434\u043e\u0431\u043d\u043e \u043b\u0435\u0433 \u043d\u0430 \u0437\u0430\u0434\u0430\u0447\u0443, \u043d\u043e \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0441 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u044b\u043c \u043a\u043e\u0434\u043e\u043c: \u0435\u0441\u043b\u0438 \u043a \u043f\u043e\u043b\u044f\u043c PRIORITY \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u0438\u0437 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u043d\u0435 \u0433\u043e\u0434\u0438\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438. \u041f\u043e\u044d\u0442\u043e\u043c\u0443, \u0438\u0434\u0435\u043c \u0434\u0430\u043b\u044c\u0448\u0435<\/p>\n<h3>\u041c\u0435\u0442\u043e\u0434 7. \u0418\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u043a\u043b\u0430\u0441\u0441\u0430 \u043f\u043e \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u043c\u0443 \u0442\u0438\u043f\u0443 \u043a\u043b\u0430\u0441\u0441\u0430 \u0438 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u0432 \u0438\u0437\u0432\u043d\u0435<\/h3>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041d\u0430\u043f\u043e\u043c\u043d\u0438\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043a\u043b\u0430\u0441\u0441\u043e\u0432<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">class Base { public:      virtual std::string getType() const = 0;      virtual const std::string&amp; getName() const = 0;      virtual const std::string getPrintedName() const = 0;      virtual ~Base() = default;  };  class File: public Base { public:      static const std::string TYPE;  public:      File(const std::string &amp;name)         : name(name)     {}      const std::string&amp; getName() const override {         return name;     }      const std::string getPrintedName() const override {         return &quot;File_&quot; + getName();     }      std::string getType() const override {         return TYPE;     }  private:      std::string name; };  const std::string File::TYPE = &quot;File&quot;;  class Folder: public Base { public:      static const std::string TYPE;  public:      Folder(const std::string &amp;name)         : name(name)     {}      const std::string&amp; getName() const override {         return name;     }      const std::string getPrintedName() const override {         return &quot;Folder_&quot; + getName();     }      std::string getType() const override {         return TYPE;     }  private:      std::string name; };  const std::string Folder::TYPE = &quot;Folder&quot;; <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0417\u0434\u0435\u0441\u044c \u043f\u043e\u043b\u0435 TYPE \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u0443\u0436\u0438\u0442\u044c \u043a\u043b\u044e\u0447\u0435\u043c \u0432 \u043a\u0430\u043a\u043e\u0439-\u043d\u0438\u0431\u0443\u0434\u044c \u043c\u0430\u043f\u0435, \u0445\u0440\u0430\u043d\u044f\u0449\u0435\u0439 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u044b. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435, \u0432 \u043a\u043e\u043c\u043f\u0430\u0440\u0430\u0442\u043e\u0440\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f \u0432 \u044d\u0442\u0443 map \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442:  <\/p>\n<pre><code class=\"cpp\">bool comparator(const std::map&lt;std::string, int&gt; &amp;priorities, const std::unique_ptr&lt;Base&gt; &amp;first, const std::unique_ptr&lt;Base&gt; &amp;second) {     const int priorityFirst = priorities.at(first-&gt;getType());     const int prioritySecond = priorities.at(second-&gt;getType());     if (priorityFirst != prioritySecond) {         return priorityFirst &gt; prioritySecond;     } else {         return first-&gt;getName() &lt; second-&gt;getName();     } } <\/code><\/pre>\n<p>  \u0417\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u0432 \u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430:  <\/p>\n<pre><code class=\"cpp\">    std::map&lt;std::string, int&gt; priorities;     priorities.emplace(Folder::TYPE, 2);     priorities.emplace(File::TYPE, 1);      std::vector&lt;std::unique_ptr&lt;Base&gt;&gt; files;     \/\/ \u0417\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 files      std::sort(files.begin(), files.end(), std::bind(comparator, std::ref(priorities), _1, _2)); <\/code><\/pre>\n<p>  <a href=\"https:\/\/github.com\/magisterRab\/sort_files_test\/blob\/master\/sort_name_external\/main.cpp\" rel=\"nofollow\">\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434<\/a><\/p>\n<h3>\u041c\u0435\u0442\u043e\u0434 8. \u0418\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u043a\u043b\u0430\u0441\u0441\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e rtti \u0441 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u043e\u0432\u0432\u0430\u043d\u0438\u0435\u043c \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u0432 \u0438\u0437\u0432\u043d\u0435<\/h3>\n<p>  \u041c\u0435\u0442\u043e\u0434 \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439, \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u044b\u0445 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f typeid \u043a\u043b\u0430\u0441\u0441\u0430  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043a\u043b\u0430\u0441\u0441\u043e\u0432<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">class Base { public:      virtual const std::string&amp; getName() const = 0;      virtual const std::string getPrintedName() const = 0;      virtual ~Base() = default;  };  class File: public Base { public:      File(const std::string &amp;name)         : name(name)     {}      const std::string&amp; getName() const override {         return name;     }      const std::string getPrintedName() const override {         return &quot;File_&quot; + getName();     }  private:      std::string name; };  class Folder: public Base { public:      Folder(const std::string &amp;name)         : name(name)     {}      const std::string&amp; getName() const override {         return name;     }      const std::string getPrintedName() const override {         return &quot;Folder_&quot; + getName();     }  private:      std::string name; }; <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u041a\u043e\u043c\u043f\u0430\u0440\u0430\u0442\u043e\u0440 \u0438 \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435:  <\/p>\n<pre><code class=\"cpp\">bool comparator(const std::unordered_map&lt;std::type_index, std::pair&lt;std::string, int&gt;&gt; &amp;priorities, const std::unique_ptr&lt;Base&gt; &amp;first, const std::unique_ptr&lt;Base&gt; &amp;second) {     const int priorityFirst = priorities.at(typeid(*first.get())).second;     const int prioritySecond = priorities.at(typeid(*second.get())).second;     if (priorityFirst != prioritySecond) {         return priorityFirst &gt; prioritySecond;     } else {         return first-&gt;getName() &lt; second-&gt;getName();     } } <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"cpp\">    std::unordered_map&lt;std::type_index, std::pair&lt;std::string, int&gt;&gt; priorities;     priorities.emplace(std::piecewise_construct, std::forward_as_tuple(typeid(Folder)), std::forward_as_tuple(&quot;Folder&quot;, 2));     priorities.emplace(std::piecewise_construct, std::forward_as_tuple(typeid(File)), std::forward_as_tuple(&quot;File&quot;, 1));      std::vector&lt;std::unique_ptr&lt;Base&gt;&gt; files;     \/\/ \u0417\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430      std::sort(files.begin(), files.end(), std::bind(comparator, std::ref(priorities), _1, _2)); <\/code><\/pre>\n<p>  <a href=\"https:\/\/github.com\/magisterRab\/sort_files_test\/blob\/master\/sort_rtti_external\/main.cpp\" rel=\"nofollow\">\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434<\/a><\/p>\n<p>  \u0417\u0434\u0435\u0441\u044c \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f unordered_map, \u0430 typeid \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0432 std::type_index \u043a\u0430\u043a \u0440\u0430\u0437 \u0434\u043b\u044f \u0442\u0430\u043a\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f.<\/p>\n<p>  \u0412 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u0433\u0434\u0435-\u0442\u043e \u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u0434\u0432\u0443\u0445 \u043c\u0435\u0442\u043e\u0434\u0430\u0445 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0438 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c\u0441\u044f. \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u044d\u0442\u0438 \u043c\u0435\u0442\u043e\u0434\u044b \u043e\u0431\u043b\u0430\u0434\u0430\u044e\u0442 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u0432\u044b\u0448\u0435 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0430\u043c\u0438, \u043d\u043e, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043f\u043b\u0430\u0442\u0430 \u0437\u0430 \u043d\u0438\u0445 \u043d\u0435 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0432\u044b\u0441\u043e\u043a\u0430. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043d\u0430\u043f\u043e\u0441\u043b\u0435\u0434\u043e\u043a \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u0434\u0435\u044e runtime-\u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 \u0441 \u0434\u0432\u043e\u0439\u043d\u043e\u0439 \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u0435\u0439<\/p>\n<h3>\u041c\u0435\u0442\u043e\u0434 9. \u041c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0441 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u0432 \u0438\u0437\u0432\u043d\u0435<\/h3>\n<p>  \u0412 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u043a\u0430\u043a \u0432\u0441\u0435\u0433\u0434\u0430 \u043d\u0438\u0447\u0435\u0433\u043e \u043b\u0438\u0448\u043d\u0435\u0433\u043e:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043a\u043b\u0430\u0441\u0441\u043e\u0432<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">class File { public:      File(const std::string &amp;name)         : name(name)     {}      const std::string&amp; getName() const {         return name;     }      const std::string getPrintedName() const {         return &quot;File_&quot; + getName();     }  private:      std::string name; };  class Folder { public:      Folder(const std::string &amp;name)         : name(name)     {}      const std::string&amp; getName() const {         return name;     }      const std::string getPrintedName() const {         return &quot;Folder_&quot; + getName();     }  private:      std::string name; };  class Symlink { public:      Symlink(const std::string &amp;name)         : name(name)     {}      const std::string&amp; getName() const {         return name;     }      const std::string getPrintedName() const {         return &quot;Symlink_&quot; + getName();     }  private:      std::string name; };  using Element = std::variant&lt;Folder, File, Symlink&gt;; <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0433\u0434\u0435-\u0442\u043e \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u044b, \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u043d\u0443 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043a\u043b\u0430\u0441\u0441\u043e\u0432. \u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430  <\/p>\n<pre><code class=\"cpp\">struct ElementPriorityBase {     virtual std::string printedType() const = 0;      virtual void setPriority(int p) = 0; }; <\/code><\/pre>\n<p>  \u042d\u0442\u043e \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441, \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043b\u044f \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u0432.<br \/>  \u0410 \u0432\u043e\u0442 \u0435\u0433\u043e \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u0438:  <\/p>\n<pre><code class=\"cpp\">template&lt;class T&gt; struct ElementPriority: public ElementPriorityBase {     int priority = 0;      std::string printedType() const override {         if constexpr (std::is_same_v&lt;T, File&gt;) {             return &quot;File&quot;;         } else if constexpr (std::is_same_v&lt;T, Folder&gt;) {             return &quot;Folder&quot;;         } else if constexpr (std::is_same_v&lt;T, Symlink&gt;) {             return &quot;Symlink&quot;;         }     }      void setPriority(int p) override {         priority = p;     } }; <\/code><\/pre>\n<p>  \u0418\u0434\u0435\u044f \u043c\u0435\u0442\u043e\u0434\u0430 \u0432 \u0447\u0435\u043c: \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c std::tuple \u0438\u0437 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u044d\u0442\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430, \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043d\u0430\u0448\u0438\u043c\u0438 File, Folder \u0438 \u0442.\u043f.  <\/p>\n<pre><code class=\"cpp\">template&lt;typename T&gt; struct Empty {};  template&lt;typename... Ts&gt; auto makeElementPriorityTupleFromElementVariant(Empty&lt;std::variant&lt;Ts...&gt;&gt;) -&gt; std::tuple&lt;ElementPriority&lt;Ts&gt;...&gt; {     return std::make_tuple(ElementPriority&lt;Ts&gt;()...); }  using ElementPriorityTuple = decltype(makeElementPriorityTupleFromElementVariant(std::declval&lt;Empty&lt;Element&gt;&gt;())); <\/code><\/pre>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f makeElementPriorityTupleFromElementVariant \u043a\u0430\u043a \u0440\u0430\u0437 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043d\u0443\u0436\u043d\u044b\u0439 \u043d\u0430\u043c tuple \u0438\u0437 \u0442\u0438\u043f\u0430 Element, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f variant-\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u043e\u0432 File, Folder,\u2026<br \/>  \u041f\u0443\u0441\u0442\u043e\u0439 \u043a\u043b\u0430\u0441\u0441 Empty \u043d\u0443\u0436\u0435\u043d \u0437\u0434\u0435\u0441\u044c \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c tuple, \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 variant-\u0430 (\u0443\u0432\u0438\u0434\u0438\u043c \u043d\u0438\u0436\u0435).<\/p>\n<p>  \u041a\u043e\u043c\u043f\u0430\u0440\u0430\u0442\u043e\u0440 \u0442\u0435\u043f\u0435\u0440\u044c \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"cpp\">template&lt;class T&gt; int priority(const ElementPriorityTuple &amp;array, const T &amp;element) {     return std::get&lt;ElementPriority&lt;T&gt;&gt;(array).priority; }  bool comparator(const ElementPriorityTuple &amp;array, const Element &amp;first, const Element &amp;second) {     const bool result = std::visit([&amp;](const auto &amp;contentFirst){         return std::visit([&amp;](const auto &amp;contentSecond){             const int priorityFirst = priority(array, contentFirst);             const int prioritySecond = priority(array, contentSecond);             if (priorityFirst != prioritySecond) {                 return priorityFirst &gt; prioritySecond;             } else {                 return contentFirst.getName() &lt; contentSecond.getName();             }         }, second);     }, first);     return result; } <\/code><\/pre>\n<p>  \u0412 \u0441\u0430\u043c\u043e\u043c \u043a\u043e\u043c\u043f\u0430\u0440\u0430\u0442\u043e\u0440\u0435 \u043d\u0435\u0442 \u043d\u0438\u0447\u0435\u0433\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0433\u043e. \u0410 \u0432 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 priority \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u043d\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 tuple, \u0442\u0430\u043a \u043a\u0430\u043a \u043c\u044b \u0437\u043d\u0430\u0435\u043c, \u043a\u0430\u043a\u0438\u043c \u0442\u0438\u043f\u043e\u043c \u044d\u0442\u043e\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u043e\u0432\u0430\u043d. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435, \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e \u043c\u0430\u043f\u0435, \u043f\u043e\u0438\u0441\u043a \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0437\u0430 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u043e\u0435 (\u0438 \u043e\u0447\u0435\u043d\u044c \u0431\u044b\u0441\u0442\u0440\u043e\u0435) \u0432\u0440\u0435\u043c\u044f.<\/p>\n<p>  \u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u0432, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043c\u043e\u0436\u043d\u043e \u043c\u0430\u043d\u0438\u043f\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u043d\u0435 \u0432\u043d\u0438\u043a\u0430\u044f \u0432 \u0434\u0435\u0442\u0430\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 ElementPriority.  <\/p>\n<pre><code class=\"cpp\">template&lt;typename tuple_t&gt; std::vector&lt;std::pair&lt;std::string, std::reference_wrapper&lt;ElementPriorityBase&gt;&gt;&gt; makeElementPriorityBaseArray(tuple_t&amp; tuple) {     constexpr auto get_array = [](auto&amp;... e){         return std::vector&lt;std::pair&lt;std::string, std::reference_wrapper&lt;ElementPriorityBase&gt;&gt;&gt;{std::make_pair(e.printedType(), std::ref(static_cast&lt;ElementPriorityBase&amp;&gt;(e)))...};     };     return std::apply(get_array, tuple); } <\/code><\/pre>\n<p>  \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435, \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043f\u0438\u0441\u043a\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"cpp\">    ElementPriorityTuple tuple = makeElementPriorityTupleFromElementVariant(Empty&lt;Element&gt;());      const std::vector&lt;std::pair&lt;std::string, std::reference_wrapper&lt;ElementPriorityBase&gt;&gt;&gt; arrayToFill = makeElementPriorityBaseArray(tuple);      for (const auto &amp;[typeStr, element]: arrayToFill) {         if (typeStr == &quot;Folder&quot;) {             element.get().setPriority(3);         } else if (typeStr == &quot;File&quot;) {             element.get().setPriority(2);         } else if (typeStr == &quot;Symlink&quot;) {             element.get().setPriority(1);         }     }      std::vector&lt;Element&gt; files = {Folder(&quot;1&quot;), File(&quot;3&quot;), File(&quot;2&quot;), Folder(&quot;4&quot;), Symlink(&quot;1&quot;), Symlink(&quot;5&quot;), Symlink(&quot;1&quot;)};      std::sort(files.begin(), files.end(), std::bind(comparator, std::ref(tuple), _1, _2));      for (const Element &amp;element: files) {         std::visit([](const auto &amp;elem) {             std::cout &lt;&lt; elem.getPrintedName() &lt;&lt; &quot; &quot;;         }, element);     } <\/code><\/pre>\n<p>  <a href=\"https:\/\/github.com\/magisterRab\/sort_files_test\/blob\/master\/sort_multiple_dispatch_external_priority\/main.cpp\" rel=\"nofollow\">\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434<\/a><\/p>\n<p>  \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u0430\u0448 tuple, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u044b, \u043f\u043e\u0442\u043e\u043c \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0438\u0437 \u043d\u0435\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u0441\u044b\u043b\u043e\u043a \u043d\u0430 \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441, \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u044b \u0438 \u0434\u0430\u043b\u044c\u0448\u0435 \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u043e \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c.<\/p>\n<h3>\u0412\u044b\u0432\u043e\u0434\u044b<\/h3>\n<p>  \u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0443\u0434\u0430\u0435\u0442\u0441\u044f \u043d\u0430\u0439\u0442\u0438 \u0445\u043e\u0440\u043e\u0448\u0435\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0439, \u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0431\u044b \u0437\u0430\u0434\u0430\u0447\u0438. \u0411\u044b\u0432\u0430\u0435\u0442, \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c\u0441\u044f \u0438 \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0438\u0437 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0435\u0441\u0442\u044c. \u042d\u0442\u043e \u043f\u043e\u0440\u043e\u0436\u0434\u0430\u0435\u0442 \u0445\u043e\u043b\u0438\u0432\u0430\u0440\u043d\u044b\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u044b \u00ab\u0430 \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u0447\u0438\u0441\u0442\u044b\u0439 \u043a\u043e\u0434\u00bb, \u00ab\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043b\u0438 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434\u00bb, \u00ab\u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u0440\u0430\u0441\u0438\u0432\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442 \u0434\u043e\u043b\u0436\u0435\u043d \u0440\u0435\u0448\u0430\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438\u00bb. \u0413\u043b\u044f\u0434\u044f \u043d\u0430 \u044d\u0442\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440, \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u043e\u0441\u043c\u044b\u0441\u043b\u0438\u0442\u044c \u043e\u0442\u0432\u0435\u0442\u044b \u043d\u0430 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u044b.<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/485882\/\"> https:\/\/habr.com\/ru\/post\/485882\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/485882\/\">\u041f\u0443\u0441\u0442\u044c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u2014 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u044b \u043a\u043b\u0430\u0441\u0441\u043e\u0432 File \u0438 Folder. \u0417\u0430\u0434\u0430\u0447\u0430 \u2014 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0441\u043f\u0438\u0441\u043e\u043a. \u0415\u0441\u0442\u044c \u0434\u0432\u0430 \u0448\u0438\u0440\u043e\u043a\u043e \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0430 \u2014 \u043a\u043e\u0433\u0434\u0430 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u0434\u0443\u0442 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0438, \u0430 \u043f\u043e\u0442\u043e\u043c \u0444\u0430\u0439\u043b\u044b, \u0438 \u043a\u043e\u0433\u0434\u0430 \u0444\u0430\u0439\u043b\u044b \u0438 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0432\u043f\u0435\u0440\u0435\u043c\u0435\u0448\u043a\u0443. \u041d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0434\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043b\u044e\u0431\u043e\u0439 \u0438\u0437 \u044d\u0442\u0438\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432.<\/p>\n<p>  \u0412\u043f\u0435\u0440\u0432\u044b\u0435 \u043d\u0430 \u043f\u043e\u0445\u043e\u0436\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u044f \u043d\u0430\u0442\u043a\u043d\u0443\u043b\u0441\u044f \u043d\u0430 \u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u0435. \u041a \u0441\u043b\u043e\u0432\u0443, \u0440\u0435\u0448\u0435\u043d\u0430 \u044d\u0442\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0431\u044b\u043b\u0430 \u043c\u0435\u0442\u043e\u0434\u043e\u043c 3 \u0438\u0437 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438. \u00ab\u041a\u0430\u043a \u043d\u0435\u044d\u0441\u0442\u0435\u0442\u0438\u0447\u043d\u043e\u00bb, \u2014 \u043f\u043e\u0434\u0443\u043c\u0430\u043b \u044f. \u2014 \u00ab\u0414\u0430\u043b\u0438 \u0431\u044b \u043c\u043d\u0435 \u044d\u0442\u0443 \u0437\u0430\u0434\u0430\u0447\u0443, \u044f \u0431\u044b \u0442\u043e\u0447\u043d\u043e \u0440\u0435\u0448\u0438\u043b \u0435\u0435 \u0432 128 \u0440\u0430\u0437 \u043b\u0443\u0447\u0448\u0435. \u041d\u043e\u2026 \u043a\u0430\u043a?\u00bb<\/p>\n<p>  \u0421 \u0442\u043e\u0439 \u043f\u043e\u0440\u044b \u043c\u0438\u043d\u0443\u043b\u043e \u0443\u0436\u0435 5 \u043b\u0435\u0442, \u0430 \u044f, \u0432\u0440\u0435\u043c\u044f \u043e\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043c\u044b\u0441\u043b\u0435\u043d\u043d\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044f\u0441\u044c \u043a \u0434\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435, \u0442\u0430\u043a \u0438 \u043d\u0435 \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u043b \u043a \u043d\u0435\u0439 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u0437\u0430 \u044d\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u0443 \u043c\u0435\u043d\u044f \u0443\u0441\u043f\u0435\u043b\u043e \u043d\u0430\u043a\u043e\u043f\u0438\u0442\u044c\u0441\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0438\u0434\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u044f \u0445\u043e\u0447\u0443 \u0441 \u0432\u0430\u043c\u0438 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-297939","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/297939","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=297939"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/297939\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=297939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=297939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=297939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}