Sorting Vector

Hi,

I'm trying to sort a vector which have other vectors as elements..

for example I have a vector - "name" with 2 strings "firstname" and 'lastname' and many of such vectors are elements of vector 'directory'. I want to sort the Vector 'directory' in order of 'firstname'.

The code I have written is as follows, I have used a for loop just to generate some entries for vector directory .

The code is not working , please help me as I have never used Comparator or Comparable Interface before..

/*

* Main.java

*

* Created on 9 February 2007, 16:49

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package javaapplication3;

import java.util.Vector;

import java.util.*;

import java.util.Collections;

/**

*

* @author amitk

*/

publicclass Mainimplements Comparator{

/** Creates a new instance of Main */

public Main(){

}

/**

* @param args the command line arguments

*/

publicstaticvoid main(String[] args){

Vector directory =new Vector();

// Just creating some entries

for (int i=0;i<=2;i++)

{

String firstName ="FirstName" + i;

String lastName ="LastName" + i;

Vector name =new Vector();

name.addElement(firstName);

name.addElement(lastName);

directory.addElement(name);

}

Collections.sort(directory);

// TODO code application logic here

}

publicint compare(Object vectorElement1, Object vectorElement2)

{

Vector tempVector1 = (Vector)vectorElement1;

String tempFirstName1 = (String)(tempVector1.elementAt(0));

Vector tempVector2 = (Vector)vectorElement2;

String tempFirstName2 = (String)(tempVector2.elementAt(0));

return tempFirstName1.compareTo(tempFirstName2);

}

}

[3059 byte] By [Amit.Kotharia] at [2007-11-26 18:03:30]
# 1

"Not working" doesn't tell us anything.

Take a look at these, and if you still have trouble, post your specific problem.

[url=http://www.onjava.com/lpt/a/3286]Making Java Objects Comparable[/url]

http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html

http://www.javaworld.com/javaworld/jw-12-2002/jw-1227-sort.html

jverda at 2007-7-9 5:33:39 > top of Java-index,Java Essentials,Java Programming...
# 2
Well all your reply has done is to encourage me and look for the answers myself..Thanks anyways...
Amit.Kotharia at 2007-7-9 5:33:39 > top of Java-index,Java Essentials,Java Programming...
# 3
Tell us how it's not working.The comparison method looks like it should work fine...Your non-static comparing method doesn't have any objects of type Main to use it. Just make it static... you don't need the class to implement anything with what you're doing.
grenceza at 2007-7-9 5:33:39 > top of Java-index,Java Essentials,Java Programming...
# 4

> Well all your reply has done is to encourage me and

> look for the answers myself..

Good. That's what you should do anyway. When you get stuck, then come here and ask a clear, specific question.

> Thanks anyways...

Adding "anyways" implies that I didn't help you, when in fact I did, by telling you what to do to have a better chance of getting an answer here and by directing your toward resources that should answer most of your questions.

Good luck.

jverda at 2007-7-9 5:33:39 > top of Java-index,Java Essentials,Java Programming...