a couple months ago we had a task in our programming class where we needed to find a solution for a specific problem:
Given are two lists A and B. A is already sorted and B contains only elements found in list A, but not necessarily all and they're not sorted. Also all elements are unique, so no duplicates in either list. For example the lists might look like this:
- List A: 1, 3, 6, 7, 8, 9, 12, 21, 84
- List B: 9, 3, 21, 84, 8
So what's this got to do with Everything? Well, a few days ago I noticed that the fast sort in Everything basically solves exactly this problem. Say you have 500.000 results from the current search term sorted by name out of 1.000.000 database elements in total. When you then click on sort by path, Everything probably uses the fast path sort index to more efficiently sort the 500.000 results, which is exactly what this problem is about. I noticed that Everything does this really really quickly (so probably no binary searching to determine the order) and there doesn't seem to be a memory spike during the sorting (so probably not our solution with a temporary hash map either), Hence I was curious how it solves that problem?
And by the way, thank you very much for this great application. It's been my daily companion for years!
Our solution: If the lists have the same length, list A is the solution, so we can just copy it over to B. Otherwise we first walk through list B and remember every element we find in a hash map. Then we walk list A and for each element we check if it's in the hash map, if yes we overwrite the nth element in list B with it and increment n by one, otherwise we ignore it. At the end B is sorted according to list A.
The drawbacks of this solution was basically the additional memory cost for the hash table, otherwise it was benchmarked to be the fastest solution in our class.