0

In my program I have a collection of some "entries" representing some files. It looks like this:

class Entries
{
  public:
    //...
  private:
    std::vector<std::filesystem::path> paths_;
    std::vector<std::string> mimeTypes_;
    std::vector<std::size_t> sizes_
    std::vector<std::filesystem::file_time_type> lastWrites_;
    //...
};

Let's say I want to sort my entries by one of the properties(path, type, size or modification time). Is there any way I can "link" all the vectors together so that when sorting one of them the elements of the others will be sorted in exactly the same way?

Alexey104
  • 969
  • 1
  • 5
  • 17
  • 4
    Is there any reason to not have `struct Entry {std::filesystem::path path; std::string mimeType; std::size_t size; std::filesystem::file_time_type lastWrite; };` and vector of those in `Entries`? – Yksisarvinen Aug 26 '21 at 15:46
  • @Yksisarvinen, performance is the reason. – Alexey104 Aug 26 '21 at 15:48
  • 3
    Add one vector of indices, sort that, and access each item indirectly? – WilliamClements Aug 26 '21 at 15:49
  • 1
    @Alexey104 [See this answer](https://stackoverflow.com/questions/46382252/sort-array-by-first-item-in-subarray-c/46382976#46382976) and take a look at the footnote in that answer. Does it fit your scenario? It seems like it. – PaulMcKenzie Aug 26 '21 at 16:23
  • @PaulMcKenzie, Thank you, I think that is exactly what I need. – Alexey104 Aug 26 '21 at 16:27
  • If there is a 1-to-1 relationship between the items in paths and mimeTypes, then this is the wrong data structure. That is, if paths[i], mimeTypes[i], sizes[i] and lastWrites[i] are all related, then this is a really awkward way to store it, and your argument of efficiency doesn't really apply. I don't know why it applies anyway. One vector vs. 4 seems a lot more efficient. – Joseph Larson Aug 26 '21 at 17:06

0 Answers0