Question on Sorted in java in LinkedList - TechRepublic
General discussion
November 21, 2004 at 08:39 AM
modi8_8

Question on Sorted in java in LinkedList

by modi8_8 . Updated 18 years, 8 months ago

My question is why sort method in LinkedList can not sort String typ and integer typ in the same list. For example if we creat list of LinkedList with integer numbers and string names
The code
in the test class Iam using Java API
LinkedList list = new LikedList();
list.add(new Integer(1));
list.add(“AAAAA”);
list.add(new Integer(6));
list.add(“BBBBB”);

//then when we print them
System.out.print(“List is :” + list);
this will be the result
List is : [1, AAAAA, 6, BBBBB]
but when we try to sort them
Collections.sort(list);
System.out.print(“Sorted list is ” + list);
the result will give me error, because sort method can not sort two diffrenet data typ in the same list
I want to know why that can not happen…. where is the exzatly problem in the sort method that make can not do this…why we can not compare name(string data typ) with number (integer data typ)
I will be greatful if any on e can help me and answer my question

This discussion is locked

All Comments