• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting data from List

 
Greenhorn
Posts: 26
Firefox Browser Tomcat Server Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 14
jQuery Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic