This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
i have one array list . it contains different datatypes value like as int,double,string
Having an ArrayList (or Array not sure from your post which it is you have) with different types is a sign of poor design and you can't mix primitive and objects types in either ArrayLists or Arrays. The primitive values will have been autoboxed to their wrapper types ie int to Integer, float to Float etc.
. now i need how to sorting this values...
You need to decide how you want to sort them eg do you want to convert them all to Strings and do a simple alphanumeric sort or something altogether more complicated.
I am not sure you can compare different type of objects in a collection. They need to be mutually comparable for you to be able to sort them. Could you be more specific? Please paste the elements you are trying to sort using code tags. And like Tony rightly pointed out, it is bad design to include different types of elements in a collection. Hence, the concept of Generics.
List<String> words = new ArrayList<String>(50);
Now, we can sort this list with a simple call to the following library method:
The Collections.sort() method will work "out of the can" on lists of various "basic" objects that you'd expect to be able to sort, including instances of Number– the primitive wrapper classes Integer, Long, Float etc plus BigInteger and BigDecimal.
Sorting an array
Sorting an array of these objects is just as easy1:
The Collections.sort() method will work "out of the can" on lists of various "basic" objects . . .
That is not really a helpful thing you have been told. It does not provide any explanation. Look at this tutorial. Numbers and Strings are kinds of what some people call value objects. Not only do value objects represent a “value” but also (in many cases) they can be “equal to”, “more than” or “less than” each other. Now you find that means they have a “natural ordering”, so if you look at the documentation for String or all those types of Number mentioned, they all implement Comparable. If you read that, you find those sort methods take Comparable as a parameter type.
I agree with everything that's been said, but I couldn't get this out of my mind for some reason. If you wanted to do something like this for real (please don't), you'd need a method to determine the type of value stored in the wrapper class and proper getters, etc. If you were addings strings that weren't string representations of numbers, you'd have to handle them in the compareTo() method appropriately of course.
Joined: Oct 13, 2005
I am gladyou said, “Please don’t”. What would happen if you had a List<Number> and passed a Comparator<Number> to the sort method?You will still have problems with Longs with > 15 digits, because their precision can exceed that of a double.
Joined: Dec 08, 2012
Campbell Ritchie wrote:I am gladyou said, “Please don’t”. What would happen if you had a List<Number> and passed a Comparator<Number> to the sort method?
You can't, my class does not extend Number.
Campbell Ritchie wrote:You will still have problems with Longs with > 15 digits, because their precision can exceed that of a double.
My class doesn't accept Longs.
I'm not saying it's without flaws, it was just an idea - as clumsy as it might be.
Joined: Oct 13, 2005
I didn’t mean that your class extends Number, I meant that my List would accept Numbers. And it was my method which would have difficulty with Longs.