How to sort a vector elements that contains the user define
class object? (Note: If Suppose consider, A Student class
contain two data members. They are String studentName and
int rollNo. I am creating Four objects for this class, each
object contains students details like name and roll no. Now
i am storing that objects in vector and if i retiving the
elements from the vector means then it should be display in
sorting order)

Answer Posted / vignesh_27

Hai The solution for this problem is.

import java.util.Collections;
import java.util.Vector;

class PointCoordinates {
private String x;
private int y;
public PointCoordinates(String x, int y) {
this.x = x;
this.y = y;
}
public String getX() {
return x;
}
public int getY() {
return y;
}
// Custom toString() Method.
public String toString() {
return x + " " + y;
}
}
public class ToStringFunction {
public static void main(String args[]) {
Vector v = new Vector();

PointCoordinates point1 = new
PointCoordinates("Rajan", 10);
PointCoordinates point2 = new
PointCoordinates("Vikky", 30);
PointCoordinates point3 = new
PointCoordinates("Zari",20);
PointCoordinates point4 = new
PointCoordinates("Ajai",80);
PointCoordinates point5 = new
PointCoordinates("Kennedi", 90);
v.addElement(point1.toString());
v.addElement(point2.toString());
v.addElement(point3.toString());
v.addElement(point4.toString());
v.addElement(point5.toString());
Collections.sort(v);
for(int i=0;i<v.size();i++)
System.out.println(v.get(i));


}
}

Is This Answer Correct ?    10 Yes 9 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is bool mean?

562


Why 1 is not a prime number?

547


What are the important features of Java 10 release?

508


Explain about core java?

618


What do you understand by an io stream?

575






What technique can be employed to compare two strings?

567


What is the latest java version?

550


what is deadlock? : Java thread

519


What is an example of declaration?

517


What language is pass by reference?

564


What is methods and methodology?

524


Is list ordered in java?

536


Can constructor be synchronized?

531


What is parsing a string?

586


Difference between final and effectively final ? Why is effectively final even required ?

548