Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting data from List

 
ssasi kumar
Greenhorn
Posts: 26
Chrome Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2941
59
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Criselda Clave
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic