Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting a Vector

Status
Not open for further replies.

SnapJack

Programmer
Aug 6, 2001
25
SG
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
 
Hi 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..

Thanks for trying
Regards
SnapJack
 
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:
Code:
v.add(new Integer(23));
Wushutwist
 
/**
* Title:
* Description:
* Copyright: Copyright (c) 2001
* Company: ÁªÏ뼯ÍÅÓÐÏÞ¹«Ë¾
* @author hongyu71@hotmail.com
* @version 1.0
*/

import java.util.*;

class MyStringComparator implements Comparator {
public int compare(Object o1, Object o2) {
int j1 = Integer.parseInt((String)o1);
int j2 = Integer.parseInt((String)o2);
return (j1 < j2 ? -1 : (j1 == j2 ? 0 : 1));
}
}

public class Testing
{
public static void main(String args[])
{
Vector v=new Vector();
v.add(&quot;23&quot;);
v.add(&quot;5&quot;);
v.add(&quot;32&quot;);
v.add(&quot;43&quot;);
v.add(&quot;6&quot;);
v.add(&quot;21&quot;);
Collections.sort(v, new MyStringComparator());
for(int i=0;i<v.size();i++)
{
System.out.println(v.get(i));
}

}

}
 
Try this.....

import java.util.Vector;

public class SortVector2 {

public static void main(String a[]){

Vector v = new Vector(5,2);

v.add(&quot;45&quot;);
v.add(&quot;5&quot;);
v.add(&quot;13&quot;);
v.add(&quot;10&quot;);
v.add(&quot;4&quot;);
v.add(&quot;13&quot;);

int temp=0;
System.out.println(&quot;The numbers are :&quot;);

for(int i=0;i<v.size();i++) {
System.out.println(v.elementAt(i));
}

for(int j=0;j<v.size();j++) {
for(int i=j;i<v.size()-1;i++) {

int num1 = Integer.parseInt((String)v.elementAt(i));
int num2 = Integer.parseInt((String)v.elementAt(i+1));
if(num1<=num2) {

temp=num1;
v.add(i, &quot;&quot;+num2);
v.remove(i+1);
v.add(i+1, &quot;&quot;+temp);
v.remove(i+1+1);
}
}
}

System.out.println(&quot;Sorted Numbers are Vector:&quot;);
for(int i=0;i<v.size();i++) {
System.out.println(v.elementAt(i));
}

}
}


Regards
Vikas Jolly
 

Try this........

import java.util.Vector;

public class SortVector {

public static void main(String a[]) {

Vector v = new Vector(5,2);

v.add(&quot;45&quot;);
v.add(&quot;5&quot;);
v.add(&quot;13&quot;);
v.add(&quot;10&quot;);
v.add(&quot;4&quot;);
v.add(&quot;13&quot;);

Integer arr[] = new Integer[v.size()];

System.out.println(&quot;The numbers are :&quot;);

for(int i=0;i<v.size();i++) {

String temp1 = (String)v.elementAt(i);
int num = Integer.parseInt(temp1);
arr = new Integer(num);
System.out.println(&quot;ARR :&quot;+arr);
}

int temp=0;
for(int j=0;j<arr.length;j++) {

for(int i=j;i<arr.length-1;i++) {

int num1 = arr.intValue();
int num2 = arr[i+1].intValue();
if(num1<=num2) {
temp=num1;
arr=new Integer(num2);
arr[i+1]=new Integer(temp);
}

}
}

Vector vt = new Vector(10,2);
for(int i=0;i<arr.length;i++) {
vt.add((&quot;&quot;+arr));
}

v = vt;
System.out.println(&quot;Sorted Numbers are Vector:&quot;);

for(int i=0;i<v.size();i++) {
System.out.println(v.elementAt(i));
}


}
}

Regards

Vikas Jolly
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top