# Sorting Of Array From Even Number To Odd Number

Deepak Sahu
Greenhorn
Posts: 23
Hi Friends ,

Please any one help to sort the below array from even to odd .

I have a array like

int a[] ={11 ,34 ,23 ,8,90,5} ;

The requirement is we have to sort the array like

int []={34,8,90.11,23,5};

Stephan van Hulst
Bartender
Posts: 6320
78
You could consider implementing a Comparator<Integer> and then calling Arrays.sort(T[] a, Comparator<? super T> c).

Luigi Plinge
Ranch Hand
Posts: 441
I don't think there's a built-in command to do that. You'd have to go through the elements and use

then copy all the odd and even numbers into a new array.

OR, you could turn the array into an ArrayList and go through the list, testing if each element is even, and if so, moving it to the start of the list. Probably easier.

Luigi Plinge
Ranch Hand
Posts: 441
But yeah, Stephan's way is the "proper" way of doing it.

Stephan van Hulst
Bartender
Posts: 6320
78
The only problem is that you would have to convert the int[] to an Integer[] first, which is sadly a bit ugly and maybe costly.
Anyway, here's a comparator you could use.

marc weber
Sheriff
Posts: 11343
Actually, I'm not sure there's a "proper" way here.

Using a Comparator still requires that you write the logic to implement, and there's the overhead of creating Integer Objects for each int, then getting primitive ints back again.

My guess is that Deepak's question is more about how to approach the logic. As Luigi suggested, one approach is to separate even and odd, then treat them as separate cases. Any other ideas...?

Stephan van Hulst
Bartender
Posts: 6320
78
Hmmm here's another example, based on what Luigi suggested:
This should actually be more efficient than the Arrays.sort() method, because that uses O(n*log(n)) time, while this uses O(n) time.

Luigi Plinge
Ranch Hand
Posts: 441
Or along the same lines without using ArrayList:

Stephan van Hulst
Bartender
Posts: 6320
78
Luigi, you may not want to use i % 2 == 1 to decide whether a number is odd. It will not work for negative values.

Use i % 2 != 0 instead.

Wouter Oet
Saloon Keeper
Posts: 2700
Be careful if you use Comparator<Integer>. Because remember that if you compare Integers (not int's) you're comparing objects. The Integer class has an Integer cache so the return values may be unexpected. A way to "solve" this is to make them int's:

Luigi Plinge
Ranch Hand
Posts: 441
Wouter Oet wrote:The Integer class has an Integer cache so the return values may be unexpected.

What sort of unexpected values??

Rob Spoor
Sheriff
Posts: 20665
65
Wouter means using ==. For all Integer objects not retrieved from the auto-boxing cache that will return false even if their values are the same:

Luigi Plinge
Ranch Hand
Posts: 441
I understand. You should use the equals() method for objects - don't have the to turn them into ints, but it would be easy to use == by mistake and think your code was working if you only tested with low numbers.

I just did a test and the same thing happens with the auto-boxing in Arraylists... this one's more confusing because Integer objects aren't even mentioned:

marc weber
Sheriff
Posts: 11343
Luigi Plinge wrote:...the same thing happens with the auto-boxing in Arraylists... this one's more confusing because Integer objects aren't even mentioned...

Unlike arrays, Collections (like Lists) cannot hold primitive values. This is why the autoboxing happens.

Although this will compile and run without mentioning Integer types, you will probably see compiler warnings about unchecked or unsafe operations. Since Java 5, the safer way is to specify a type for the Collection...

...this allows the compiler to insert all the necessary casting and avoid surprises about what your Collection actually contains.