Tuesday, July 19, 2011

Don't use RecordComparator

Even if you try to keep the number of classes to a minimum you must create some classes to represent your entities. More so if you use these entities on IO operations: save on recordStore; send on httpRequest; etc.

This tip is for those who do not use/know Floggy and create methods to [de]serialize these entity classes from/to byte arrays:
Do not use RecordComparator

Why? Because this interface defines a compare method that receives two byte arrays, you should know how your entities are represented as byte arrays and compare them only with array indexing. If you deserialize a class only to compare it you will be wasting a load of memory only to sort your RecordEnumeration.

I consider a better approach to load your RecordStore without a RecordComparator, deserialize your class and use an insertion sort algorithm on a Vector.

Related topics: