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?
CodeForTea@gmail.com - 01 Apr 2007 03:03 GMT
> hello,
>
[quoted text clipped - 20 lines]
>
> 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
black-white - 01 Apr 2007 10:31 GMT
Hello,
You misunderstood my question but thanks for answer anyway...
>> hello,
>>
[quoted text clipped - 57 lines]
> }
> Dinesh
Hendrik Maryns - 02 Apr 2007 10:52 GMT
black-white schreef:
> Hello,
>
> You misunderstood my question but thanks for answer anyway...
Please do not top-post.
I think /you/ misunderstood Dinesh’s solution. Have another look at it,
it does exactly what you asked. (You might want to have a look at
anonymous classes too, you can use one that implements Comparator in
Collections.sort(List, Comparator))
H.
>>> hello,
>>>
[quoted text clipped - 57 lines]
>> }
>> Dinesh
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Patricia Shanahan - 01 Apr 2007 03:50 GMT
> hello,
>
[quoted text clipped - 20 lines]
>
> Now, how can I sort that list just considering value1 in all objects?
If the value1 order is the natural order for class a, make it implement
Comparable, and use the Collections sorts for lists of Comparable.
If there are several equally significant orders, create a Comparator for
each order, and use the Collections sorts with Comparator.
Patricia
Muntasir Azam Khan - 01 Apr 2007 09:34 GMT
> 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?
...
> Now, how can I sort that list just considering value1 in all objects?
Look up the Comparable and Comparator interfaces.
If you only need to sort on one value make your class implement
Comparable.
If you need to sort on different values at different times, create a
separate Comparator (i.e. a class that implements the Comparator
interface) for each value you are sorting on.
black-white - 01 Apr 2007 10:32 GMT
Muntasir and Patricia,
Thanks I will look up those interfaces...
>> hello,
>>
[quoted text clipped - 10 lines]
> separate Comparator (i.e. a class that implements the Comparator
> interface) for each value you are sorting on.