Fix bug in torrent file not enforcing order to final manifests.

Change-Id: I8ee6243a8e25b305db719fa16b5e653f12ef9cdc
diff --git a/src/torrent-file.cpp b/src/torrent-file.cpp
index dafeb41..d7918d2 100644
--- a/src/torrent-file.cpp
+++ b/src/torrent-file.cpp
@@ -257,15 +257,17 @@
   Name torrentName(commonPrefix.toUri() + "/torrent-file");
   TorrentFile currentTorrentFile(torrentName, commonPrefix, {});
   std::vector<std::pair<std::vector<FileManifest>, std::vector<Data>>> manifestPairs;
-
+  // sort all the file names lexicographically
+  std::set<std::string> fileNames;
+  for (auto i = directoryPtr; i != fs::recursive_directory_iterator(); ++i) {
+    fileNames.insert(i->path().string());
+  }
   size_t manifestFileCounter = 0u;
-  for (fs::recursive_directory_iterator i = directoryPtr;
-       i != fs::recursive_directory_iterator();
-       ++i) {
+  for (const auto& fileName : fileNames) {
     Name manifestPrefix("/NTORRENT" +
                         directoryPathName.getSubName(directoryPathName.size() - 1).toUri());
     std::pair<std::vector<FileManifest>, std::vector<Data>> currentManifestPair =
-                                                    FileManifest::generate((*i).path().string(),
+                                                    FileManifest::generate(fileName,
                                                     manifestPrefix, subManifestSize,
                                                     dataPacketSize, returnData);
 
diff --git a/tests/unit-tests/torrent-file.t.cpp b/tests/unit-tests/torrent-file.t.cpp
index 83e1f2a..4ab3077 100644
--- a/tests/unit-tests/torrent-file.t.cpp
+++ b/tests/unit-tests/torrent-file.t.cpp
@@ -418,10 +418,13 @@
       // load  the contents of the directory files from disk
       std::vector<uint8_t> directoryFilesBytes;
       fs::recursive_directory_iterator directoryPtr2(fs::system_complete(directoryPath).string());
-      for (fs::recursive_directory_iterator j = directoryPtr2;
-           j != fs::recursive_directory_iterator();
-           ++j) {
-        fs::ifstream is((*j), fs::ifstream::binary | fs::ifstream::in);
+
+      std::set<std::string> fileNames;
+      for (auto j = directoryPtr2; j != fs::recursive_directory_iterator(); ++j)  {
+        fileNames.insert(j->path().string());
+      }
+      for (const auto& fileName : fileNames) {
+        fs::ifstream is(fileName, fs::ifstream::binary | fs::ifstream::in);
         is >> std::noskipws;
         std::istream_iterator<uint8_t> start(is), end;
         std::vector<uint8_t> fileBytes(start, end);