working with lists

B

black-white

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?
 
C

CodeForTea

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
 
P

Patricia Shanahan

black-white said:
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?

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
 
M

Muntasir Azam Khan

black-white said:
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.
 
B

black-white

Hello,

You misunderstood my question but thanks for answer anyway...



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
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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,

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


- --
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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQFGENaDe+7xMGD3itQRAki/AJ9bgRgNMF5qvA/FnQHcjZAk7U5ySwCfcMdW
5sST2P2WjNfYGdqZyPASGes=
=vI38
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top