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
What is bool mean?
Why 1 is not a prime number?
What are the important features of Java 10 release?
Explain about core java?
What do you understand by an io stream?
What technique can be employed to compare two strings?
What is the latest java version?
what is deadlock? : Java thread
What is an example of declaration?
What language is pass by reference?
What is methods and methodology?
Is list ordered in java?
Can constructor be synchronized?
What is parsing a string?
Difference between final and effectively final ? Why is effectively final even required ?