matched icon indicating copy to clipboard operation
matched copied to clipboard

Ordering of results

Open arshaw opened this issue 6 years ago • 1 comments
trafficstars

Hello! It seems as though the ordering of results is non-deterministic, right? This seems true to me, for two reasons:

  • the nosort flag is set to true when asking glob to return results (https://github.com/jonschlinkert/matched/blob/master/lib/promise.js#L12)
  • the aggregate result of glob calls seems to be constructed in the order they are returned, which is unpredictable (https://github.com/jonschlinkert/matched/blob/4.0.0/lib/promise.js#L30)

It this the intended behavior? If so, would you mind mentioning something about it in the docs? Thanks for this lib!

arshaw avatar Aug 01 '19 19:08 arshaw

Hello! It seems as though the ordering of results is non-deterministic, right?

Directories are looped over by node-glob asynchronously, thus files may be returned in any order, depending on which directories finish first.

  • the nosort flag is set to true when asking glob to return results

That's only a default that may be overridden on options. We do this because node-glob sorts results by default, and we don't want glob to perform extra unnecessary operations.

the aggregate result of glob calls seems to be constructed in the order they are returned, which is unpredictable

files.sort()

Edit: I hope that last line didn't seem snarky without a comment, I was in a rush and should have taken more time. I just wanted to make the point that .sort() is all you need to do to sort the results. There will never be a time when files are deterministic in a realistic scenario -- even when directories are looped over synchronously, since files and folders change. However, if you're just looking for determinism for unit tests or something, you should be able to simulate that by sorting the returned array. I had considered adding this feature, but honestly all we'd do is files.sort(). If consensus is that we should do it anyway, for parity, I'm totally fine with that and would accept a PR.

Thanks for the issue.

jonschlinkert avatar Jan 15 '20 20:01 jonschlinkert