Hello ,
Lets say i have a vector with the values 1,5,22,4,3.
I want to sort this vector in ascending order..
I want the sorting to be done based on the Ascii values so that i can use it on Strings too..
please help...
Regards
Snapjack
Use the Collections.sort(List list) method. By defaults it sorts into ascending order. If you need to change the way it sorts use Collections.sort(List list, Comparator c) and pass it a Comparator. Wushutwist
Thats not what exactly i am looking for..see the problem lies here...
import java.util.*;
public class Testing
{
public static void main(String args[])
{
Vector v=new Vector();
v.add("23"
v.add("5"
v.add("32"
v.add("43"
v.add("6"
v.add("21"
Collections.sort(v);
for(int i=0;i<v.size();i++)
{
System.out.println(v.get(i));
}
}
}
Result:
21
23
32
43
5
6
The result is not in ascending order coz the comparison goes on the basis of the first character...
so u get the problem...
Please try to give me a solution asap..
That is what the Collections.sort(List list, Comparator c) method is for. You can define a class that implements java.util.Comparator and pass it to the method. Wushutwist
The other solution is if the elements don't really need to be String you can forget about the Comparator and add all the elements to Vector as Integers.
Example:
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.