If you ever wondered how can you compare 2 objects (big structure objects that can not be compared by Flex itself using the === operator) in Flex to find out if they are identically the same one of the methods used were to check all their attributes.
Another much better and faster method is this:
public function compareObject(obj1:Object,obj2:Object):Boolean { var buffer1:ByteArray = new ByteArray(); buffer1.writeObject(obj1); var buffer2:ByteArray = new ByteArray(); buffer2.writeObject(obj2); // compare the lengths var size:uint = buffer1.length; if (buffer1.length == buffer2.length) { buffer1.position = 0; buffer2.position = 0; // then the bits while (buffer1.position < size) { var v1:int = buffer1.readByte(); if (v1 != buffer2.readByte()) { return false; } } return true; } return false; }
Tags: ActionScript, flex, RIA
This post was written by Virgil Cristea
Views: 5586










Hi,
I tried this piece of code but it always returns false.
I have two really big objects which I like to compare.
kind regards
Theo
What this method does it serializes the objects and then compares them on byte basis. It should work for all objects. Can you please send me an email with what you are trying to compare?
Great function!
Cut n paste it and it works flawlessly in Flex 3.
This replaced a === that wasn’t working for me. Has anyone found a simpler way to do the same thing?
This looks nice. I’ll have to try it out.
Thanks in advance…