Skip to content

Model Object

compareTo()

compareTo() — returns boolean

Available in: model Category: Miscellaneous Functions

Compares the current model object with another model object to determine if they are effectively the same. This is useful for checking equality between two instances of the same model before performing operations like updates or merges.

NameTypeRequiredDefaultDescription
objectcomponentyes
1. Compare two user objects
user1 = model("user").findByKey(1);
user2 = model("user").findByKey(2);

if(user1.compareTo(user2)) {
    writeOutput("Objects are the same.");
} else {
    writeOutput("Objects are different.");
}

2. Compare dynamically after changing a property
user1 = model("user").findByKey(1);
user2 = model("user").findByKey(1);

user2.email = "[newemail@example.com](mailto:newemail@example.com)";

writeDump(user1.compareTo(user2)); // Will output false because email changed