aspose file tools*
The moose likes Beginning Java and the fly likes Sorting data from List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting data from List" Watch "Sorting data from List" New topic
Author

Sorting data from List

ssasi kumar
Greenhorn

Joined: Feb 23, 2011
Posts: 26

hi every one,

i have one array list . it contains different datatypes value like as int,double,string. now i need how to sorting this values...

regards
sasikumar
Michal Plutynski
Greenhorn

Joined: Mar 01, 2012
Posts: 14

Hi,
this is very interesting question. Can you please tell me what are your criterias for sorting? Should numbers be before letters?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2312
    
  49
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.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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.


~ Mansukh
Criselda Clave
Greenhorn

Joined: Mar 07, 2013
Posts: 6
i don't know its run that code.. but you try it..

List<String> words = new ArrayList<String>(50);
words.add("once");
words.add("upon");
words.add("a");
words.add("time");
...

Now, we can sort this list with a simple call to the following library method:

Collections.sort(words);

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:

Arrays.sort(myArray);
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi Criselda..

Always add code tags while pasting code like this:



Looks much better doesn't it?

And yes, a warm welcome.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
Criselda Clave wrote: . . .
Collections.sort(words);

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.
Steve Myers
Ranch Hand

Joined: Dec 08, 2012
Posts: 47
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.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
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.
Steve Myers
Ranch Hand

Joined: Dec 08, 2012
Posts: 47
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting data from List