diff --git a/src/lib-sieve/sieve-script-private.h b/src/lib-sieve/sieve-script-private.h
index 19396274dc3aae39cd9f6dc0e965e178fe7fa22d..6b86285c289b10f51755262890cae6db197b7ed2 100644
--- a/src/lib-sieve/sieve-script-private.h
+++ b/src/lib-sieve/sieve-script-private.h
@@ -46,8 +46,8 @@ struct sieve_script_vfuncs {
 	int (*get_size)(const struct sieve_script *script, uoff_t *size_r);
 
 	/* matching */
-	bool (*equals)(const struct sieve_script *script,
-		       const struct sieve_script *other);
+	int (*cmp)(const struct sieve_script *script,
+		   const struct sieve_script *other);
 };
 
 struct sieve_script {
diff --git a/src/lib-sieve/sieve-script.c b/src/lib-sieve/sieve-script.c
index 3029d0e907faa21e62afa96ac1e5ec16f9ce171d..b3fb348c3d834cd86cb623c25b789c0cb7bf6e79 100644
--- a/src/lib-sieve/sieve-script.c
+++ b/src/lib-sieve/sieve-script.c
@@ -358,23 +358,23 @@ int sieve_script_get_stream(struct sieve_script *script,
  * Comparison
  */
 
-bool sieve_script_equals(const struct sieve_script *script,
-			 const struct sieve_script *other)
+int sieve_script_cmp(const struct sieve_script *script,
+		     const struct sieve_script *other)
 {
 	if (script == other)
-		return TRUE;
+		return 0;
 	if (script == NULL || other == NULL)
-		return FALSE;
+		return (script == NULL ? -1 : 1);
 	if (script->script_class != other->script_class)
-		return FALSE;
+		return (script->script_class > other->script_class ? 1 : -1);
 
-	if (script->v.equals == NULL) {
+	if (script->v.cmp == NULL) {
 		i_assert (script->location != NULL && other->location != NULL);
 
-		return (strcmp(script->location, other->location) == 0);
+		return strcmp(script->location, other->location);
 	}
 
-	return script->v.equals(script, other);
+	return script->v.cmp(script, other);
 }
 
 unsigned int sieve_script_hash(const struct sieve_script *script)
diff --git a/src/lib-sieve/sieve-script.h b/src/lib-sieve/sieve-script.h
index 0be7cc334808c4f4e323bea96e5416d602c5fd28..25a5736e7ef244e040c55f5dfdb8907924317c3f 100644
--- a/src/lib-sieve/sieve-script.h
+++ b/src/lib-sieve/sieve-script.h
@@ -127,17 +127,17 @@ sieve_file_script_get_path(const struct sieve_script *script) ATTR_PURE;
  * Comparison
  */
 
-bool sieve_script_equals(const struct sieve_script *script,
-			 const struct sieve_script *other);
-
-unsigned int sieve_script_hash(const struct sieve_script *script);
-static inline int
-sieve_script_cmp(const struct sieve_script *script,
-		 const struct sieve_script *other)
+int sieve_script_cmp(const struct sieve_script *script,
+		     const struct sieve_script *other);
+static inline bool
+sieve_script_equals(const struct sieve_script *script,
+		    const struct sieve_script *other)
 {
-	return ( sieve_script_equals(script, other) ? 0 : -1 );
+	return (sieve_script_cmp(script, other) == 0);
 }
 
+unsigned int sieve_script_hash(const struct sieve_script *script);
+
 /*
  * Error handling
  */
diff --git a/src/lib-sieve/storage/data/sieve-data-script.c b/src/lib-sieve/storage/data/sieve-data-script.c
index 935aa02085acc146f3fcf3d722222e01eca0eafa..3bc6774cdf4b79b97b7d1d6eaacbc4a3b6911d84 100644
--- a/src/lib-sieve/storage/data/sieve-data-script.c
+++ b/src/lib-sieve/storage/data/sieve-data-script.c
@@ -80,20 +80,11 @@ sieve_data_script_get_stream(struct sieve_script *script,
 	return 0;
 }
 
-static bool
-sieve_data_script_equals(const struct sieve_script *script ATTR_UNUSED,
-			 const struct sieve_script *other ATTR_UNUSED)
-{
-	return (script == other);
-}
-
 const struct sieve_script sieve_data_script = {
 	.driver_name = SIEVE_DATA_STORAGE_DRIVER_NAME,
 	.v = {
 		.destroy = sieve_data_script_destroy,
 
 		.get_stream = sieve_data_script_get_stream,
-
-		.equals = sieve_data_script_equals
 	},
 };
diff --git a/src/lib-sieve/storage/dict/sieve-dict-script.c b/src/lib-sieve/storage/dict/sieve-dict-script.c
index 61bc31d6cd19d177f8fdcd87c951bdda026bea82..167fe8fe907fde32eff0ccc7644dd8b12819e678 100644
--- a/src/lib-sieve/storage/dict/sieve-dict-script.c
+++ b/src/lib-sieve/storage/dict/sieve-dict-script.c
@@ -256,19 +256,24 @@ sieve_dict_script_binary_save(struct sieve_script *script,
 				 error_code_r);
 }
 
-static bool
-sieve_dict_script_equals(const struct sieve_script *script,
-			 const struct sieve_script *other)
+static int
+sieve_dict_script_cmp(const struct sieve_script *script,
+		      const struct sieve_script *other)
 {
 	struct sieve_storage *storage = script->storage;
 	struct sieve_storage *sother = other->storage;
+	int ret;
 
-	if (strcmp(storage->location, sother->location) != 0)
-		return FALSE;
+	ret = strcmp(storage->location, sother->location);
+	if (ret != 0)
+		return (ret < 0 ? -1 : 1);
 
 	i_assert(script->name != NULL && other->name != NULL);
 
-	return (strcmp(script->name, other->name) == 0);
+	ret = strcmp(script->name, other->name);
+	if (ret != 0)
+		return (ret < 0 ? -1 : 1);
+	return 0;
 }
 
 const struct sieve_script sieve_dict_script = {
@@ -286,7 +291,7 @@ const struct sieve_script sieve_dict_script = {
 		.binary_load = sieve_dict_script_binary_load,
 		.binary_save = sieve_dict_script_binary_save,
 
-		.equals = sieve_dict_script_equals,
+		.cmp = sieve_dict_script_cmp,
 	},
 };
 
diff --git a/src/lib-sieve/storage/file/sieve-file-script.c b/src/lib-sieve/storage/file/sieve-file-script.c
index 5e1e44b7fcf5c3e28e295b8d6c0c6c46f0cba098..5f856f9e3a889886bc149f06fbd10b18aab204fa 100644
--- a/src/lib-sieve/storage/file/sieve-file-script.c
+++ b/src/lib-sieve/storage/file/sieve-file-script.c
@@ -831,28 +831,41 @@ const char *sieve_file_script_get_path(const struct sieve_script *script)
  * Matching
  */
 
-static bool
-sieve_file_script_equals(const struct sieve_script *script,
-			 const struct sieve_script *other)
+static int
+sieve_file_script_cmp(const struct sieve_script *script,
+		      const struct sieve_script *other)
 {
 	const struct sieve_file_script *fscript =
 		container_of(script, const struct sieve_file_script, script);
 	const struct sieve_file_script *fother =
 		container_of(other, const struct sieve_file_script, script);
+	int ret;
 
 	if (!script->open || !other->open) {
 		struct sieve_storage *storage = script->storage;
 		struct sieve_storage *sother = other->storage;
 
-		if (strcmp(storage->location, sother->location) != 0)
-			return FALSE;
+		ret = strcmp(storage->location, sother->location);
+		if (ret != 0)
+			return ret;
 
 		i_assert(script->name != NULL && other->name != NULL);
-		return (strcmp(script->name, other->name) == 0);
+		return strcmp(script->name, other->name);
 	}
 
-	return (CMP_DEV_T(fscript->st.st_dev, fother->st.st_dev) &&
-		fscript->st.st_ino == fother->st.st_ino);
+	if (major(fscript->st.st_dev) != major(fother->st.st_dev)) {
+		return (major(fscript->st.st_dev) > major(fother->st.st_dev) ?
+			1 : -1);
+	}
+	if (minor(fscript->st.st_dev) != minor(fother->st.st_dev)) {
+		return (minor(fscript->st.st_dev) > minor(fother->st.st_dev) ?
+			1 : -1);
+	}
+
+	if (fscript->st.st_ino != fother->st.st_ino)
+		return (fscript->st.st_ino > fother->st.st_ino ? 1 : -1);
+
+	return 0;
 }
 
 /*
@@ -878,6 +891,6 @@ const struct sieve_script sieve_file_script = {
 
 		.get_size = sieve_file_script_get_size,
 
-		.equals = sieve_file_script_equals
+		.cmp = sieve_file_script_cmp,
 	}
 };
diff --git a/src/lib-sieve/storage/ldap/sieve-ldap-script.c b/src/lib-sieve/storage/ldap/sieve-ldap-script.c
index a025a043e9440e9865d2a59dd7df4569cde52b5f..6bea3e7aaaab2aab0e22892a0bf4335130bb2fe7 100644
--- a/src/lib-sieve/storage/ldap/sieve-ldap-script.c
+++ b/src/lib-sieve/storage/ldap/sieve-ldap-script.c
@@ -279,19 +279,18 @@ sieve_ldap_script_binary_save(struct sieve_script *script,
 				 error_code_r);
 }
 
-static bool
-sieve_ldap_script_equals(const struct sieve_script *script,
-			 const struct sieve_script *other)
+static int
+sieve_ldap_script_cmp(const struct sieve_script *script,
+		      const struct sieve_script *other)
 {
-	struct sieve_storage *storage = script->storage;
-	struct sieve_storage *sother = other->storage;
-
-	if (strcmp(storage->location, sother->location) != 0)
-		return FALSE;
+	int ret;
 
 	i_assert(script->name != NULL && other->name != NULL);
 
-	return (strcmp(script->name, other->name) == 0);
+	ret = strcmp(script->name, other->name);
+	if (ret != 0)
+		return (ret > 0 ? 1 : -1);
+	return 0;
 }
 
 const struct sieve_script sieve_ldap_script = {
@@ -308,7 +307,7 @@ const struct sieve_script sieve_ldap_script = {
 		.binary_load = sieve_ldap_script_binary_load,
 		.binary_save = sieve_ldap_script_binary_save,
 
-		.equals = sieve_ldap_script_equals,
+		.cmp = sieve_ldap_script_cmp,
 	},
 };