Repository: voxel51/fiftyone
Issue: [BUG] Error using the ordered kwarg in select_by method

Calling `select_by(..., ordered=True)` results in the following error:
```
OperationFailure: Failed to optimize pipeline :: caused by :: First operand of $indexOfArray must be an array. First argument is of type: string, full error: {'ok': 0.0, 'errmsg': 'Failed to optimize pipeline :: caused by :: First operand of $indexOfArray must be an array. F
irst argument is of type: string', 'code': 50809, 'codeName': 'Location50809'}     
```

Commands to reproduce:
```python
import fiftyone. zoo as foz
import fiftyone as fo

dataset = foz.load_zoo_dataset("quickstart")

filepaths = dataset.values("filepath")[:10]
dataset.select_by("filepath", filepaths, ordered=True)
```


The following is a workaround for the time being involving setting the order of the a list, then sorting by that list:
```python
import fiftyone. zoo as foz
import fiftyone as fo

dataset = foz.load_zoo_dataset("quickstart")

filepaths = dataset.values("filepath")[:10]

import random
random.shuffle(filepaths)

order = {f:i for i, f in enumerate(filepaths)}
dataset.set_values("order", order, key_field="filepath")

view = dataset.select_by("filepath", filepaths).sort_by("order")

# Do stuff

dataset.delete_sample_field("order")
```

Changed files:
fiftyone/core/stages.py, tests/unittests/view_tests.py

diff --git a/fiftyone/core/stages.py b/fiftyone/core/stages.py
index 2470dc1a8..5889b1904 100644
--- a/fiftyone/core/stages.py
+++ b/fiftyone/core/stages.py
@@ -4477,7 +4477,11 @@ class SelectBy(ViewStage):
             return [{"$match": {path: {"$in": values}}}]
 
         return [
-            {"$set": {"_select_order": {"$indexOfArray": [path, "$" + path]}}},
+            {
+                "$set": {
+                    "_select_order": {"$indexOfArray": [values, "$" + path]}
+                }
+            },
             {"$match": {"_select_order": {"$gt": -1}}},
             {"$sort": {"_select_order": 1}},
             {"$unset": "_select_order"},
diff --git a/tests/unittests/view_tests.py b/tests/unittests/view_tests.py
index 6d196ad2d..c4edb8d96 100644
--- a/tests/unittests/view_tests.py
+++ b/tests/unittests/view_tests.py
@@ -2242,6 +2242,27 @@ class ViewStageTests(unittest.TestCase):
         for sample, _id in zip(view, ids):
             self.assertEqual(sample.id, _id)
 
+    def test_select_by(self):
+        filepaths = self.dataset.values("filepath")
+
+        values = [filepaths[1], filepaths[0]]
+        unordered_values = [filepaths[0], filepaths[1]]
+
+        result = self.dataset.select_by("filepath", values)
+
+        self.assertEqual(len(result), 2)
+        self.assertEqual(result.values("filepath"), unordered_values)
+
+    def test_select_by_ordered(self):
+        filepaths = self.dataset.values("filepath")
+
+        values = [filepaths[1], filepaths[0]]
+
+        result = self.dataset.select_by("filepath", values, ordered=True)
+
+        self.assertEqual(len(result), 2)
+        self.assertEqual(result.values("filepath"), values)
+
     def test_select_fields(self):
         self.dataset.add_sample_field("select_fields_field", fo.IntField)