You misunderstood my question but thanks for answer anyway...
On Mar 31, 9:10 pm, black-white <black.white.1...@gmail.com> wrote:
hello,
I use Vector or ArrayList to store my objects.
However, sometimes I need to sort those lists with considering just one
value. Is that possible?
For example:
Class a{
int value1;
int value2;
String name;}
Vector myList=new Vector();
a object1=new a();
object1.fillValues(); //that gives some values to each variable...
myList.add(object1);
myList.add(object2);
.
.
.....
myList.add(object1000);
Now, how can I sort that list just considering value1 in all objects?
import java.util.ArrayList;
import java.util.Collections;
public class SortOnOneValue implements Comparable {
int value1;
int value2;
public SortOnOneValue(int val1, int val2) {
value1 = val1;
value2 = val2;
}
public int compareTo(Object obj) {
SortOnOneValue val = (SortOnOneValue) obj;
return this.value1 - val.value1;
}
public String toString() {
return "value1 = " + value1 + " value2 = " + value2;
}
/**
* @param args
*/
public static void main(String[] args) {
ArrayList myList =new ArrayList();
myList.add(new SortOnOneValue(1,5));
myList.add(new SortOnOneValue(5,5));
myList.add(new SortOnOneValue(2,5));
System.out.println(myList);
Collections.sort(myList);
System.out.println(myList);
}
}
Dinesh