Will Serialization change sort order?

T

timasmith

I am noticiing some inconsitencies but cant pin them down. Perhaps the
answer to this question can help.

If I have a vector of objects which on the server side I executed
Collections.sort(vector);

I then sent the vector over the network to the client (serialized I
assume).

Will the sort order be guaranteed to be the same in the Vector?

or should I always sort on the other end...?

thanks

Tim
 
M

Mike Schilling

I am noticiing some inconsitencies but cant pin them down. Perhaps the
answer to this question can help.

If I have a vector of objects which on the server side I executed
Collections.sort(vector);

I then sent the vector over the network to the client (serialized I
assume).

Will the sort order be guaranteed to be the same in the Vector?

or should I always sort on the other end...?

Serialization and deserialization will not change the order of objects in a
vector.

However, depending on what criteria you use to sort the items, perhaps they
should be in a different order in the two processes. For instance, if you
sort Objects by the value of their hashCode(), this will almost certainly
give different results. If you sort Strings and the two processes have
different default locales, that might result in differences as well.
 
D

Daniel Dyer

I am noticiing some inconsitencies but cant pin them down. Perhaps the
answer to this question can help.

If I have a vector of objects which on the server side I executed
Collections.sort(vector);

I then sent the vector over the network to the client (serialized I
assume).

Will the sort order be guaranteed to be the same in the Vector?

or should I always sort on the other end...?

A Vector is a List (java.util.List), which, unlike a Set, guarantees the
order of its elements. So if the order is not the same after
serialisation/deserialisation, then something is broken.

Dan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top