Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to sort a object vector by its integer item ?

807603Jul 16 2006 — edited Dec 21 2007
hi everybody,

[there were few topics on this in the forum, but nothing could solve my problem yet. so, please instruct me]

I have to sort a vector which contains objects, where each object represents, different data types of values,
ex: {obj1, obj2, obj3, ....}
obj1---->{String name, int ID, String[] departments}

i have to sort this vector at three times, once by name, then by ID and then by departments.
Leaving name and department , first i want to sort the ID of each object and then re-arrange the order of objects in the array according to new order.

what i did was, copied the vector all objects' ID values to an integer array then i sorted it using selection sort. but now i want to re-arrange the vector, but i still can't. please guide.

here is the sort i did, and the
int[] ID = new int[mintomaxID.size()];
		for(int i=0;i<mintomaxID.size();i++)
		{
			ObjectStore_initialData obj_id = (ObjectStore_initialData)mintomaxID.elementAt(i);
			ID[i] = obj_id.getID(); 
		}
		
		System.out.println("Before sorting array");
		for(int i=0;i<ID.length;i++)
		{	
			System.out.println(ID);	
}
System.out.println();

int i, j, m, mi;
for (i = 0; i < ID.length - 1; i++) {
/* find the minimum */
mi = i;
for (j = i+1; j < ID.length; j++) {
if (ID[j] < ID[mi]) {
mi = j;
}
}
m = ID[mi];
/* move elements to the right */
for (j = mi; j > i; j--) {
ID[j] = ID[j-1];
}
ID[i] = m;
}

System.out.println("After sorting array");
for(int y=0;y<ID.length;y++)
{
System.out.println(ID[y]);
}

//*****here is where i need to re-arrange the entire vector by ID.
****/


I do understand this is some sort of database sort type of a question. but there's no database. it's simply i just want to sort the vector.

Thank you.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 18 2008
Added on Jul 16 2006
11 comments
419 views