diff --git a/__tests__/actionUtils.test.ts b/__tests__/actionUtils.test.ts
index e36a6a5..ae4089f 100644
--- a/__tests__/actionUtils.test.ts
+++ b/__tests__/actionUtils.test.ts
@@ -215,6 +215,36 @@ test("getInputAsArray handles empty lines correctly", () => {
     expect(actionUtils.getInputAsArray("foo")).toEqual(["bar", "baz"]);
 });
 
+test("getInputAsArray sorts files correctly", () => {
+    testUtils.setInput("foo", "bar\n!baz\nwaldo\nqux\nquux\ncorge\ngrault\ngarply");
+    expect(actionUtils.getInputAsArray("foo")).toEqual([
+        "!baz",
+        "bar",
+        "corge",
+        "garply",
+        "grault",
+        "quux",
+        "qux",
+        "waldo"
+    ]);
+});
+
+test("getInputAsArray removes spaces after ! at the beginning", () => {
+    testUtils.setInput(
+        "foo",
+        "!   bar\n!  baz\n! qux\n!quux\ncorge\ngrault! garply\n!\r\t waldo"
+    );
+    expect(actionUtils.getInputAsArray("foo")).toEqual([
+        "!bar",
+        "!baz",
+        "!quux",
+        "!qux",
+        "!waldo",
+        "corge",
+        "grault! garply"
+    ]);
+});
+
 test("getInputAsInt returns undefined if input not set", () => {
     expect(actionUtils.getInputAsInt("undefined")).toBeUndefined();
 });
diff --git a/__tests__/restore.test.ts b/__tests__/restore.test.ts
index e9a505b..6c02082 100644
--- a/__tests__/restore.test.ts
+++ b/__tests__/restore.test.ts
@@ -147,7 +147,7 @@ test("restore with no key", async () => {
 test("restore with too many keys should fail", async () => {
     const path = "node_modules";
     const key = "node-test";
-    const restoreKeys = [...Array(20).keys()].map(x => x.toString());
+    const restoreKeys = [...Array(20).keys()].map(x => x.toString()).sort();
     testUtils.setInputs({
         path: path,
         key,
diff --git a/src/utils/actionUtils.ts b/src/utils/actionUtils.ts
index 69e0220..5301bef 100644
--- a/src/utils/actionUtils.ts
+++ b/src/utils/actionUtils.ts
@@ -61,8 +61,9 @@ export function getInputAsArray(
     return core
         .getInput(name, options)
         .split("\n")
-        .map(s => s.trim())
-        .filter(x => x !== "");
+        .map(s => s.replace(/^\!\s+/, '!').trim())
+        .filter(x => x !== "")
+        .sort();
 }
 
 export function getInputAsInt(