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)

Answers were Sorted based on User's Feedback



How to sort a vector elements that contains the user define class object? (Note: If Suppose consid..

Answer / kalyan g

other way to achieve this is implementing comparator
interface and implement compare(object) method.

Is This Answer Correct ?    7 Yes 1 No

How to sort a vector elements that contains the user define class object? (Note: If Suppose consid..

Answer / naren reddy

For sorting Any user defined class,We need to implement the
userdefined class with comparable or comparator
interfcae.Then only your collections.sort(ArrayList al)
will work,Otherwise it won't work.


Ex: class Employee implements Comparable{

private int age;

public void setAge(int age){
this.age=age;
}

public int getAge(){
return this.age;
}public int compareTo(Object otherEmployee){

/*
If passed object is of type other than Employee,
throw ClassCastException.
*/

if(!(otherEmployee instanceof Employee)){
throw new ClassCastException("Invalid object");
}

int age = ((Employee) otherEmployee).getAge();

if(this.getAge() > age)
return 1;
else if ( this.getAge() < age )
return -1;
else
return 0;

}

}

Is This Answer Correct ?    2 Yes 0 No

How to sort a vector elements that contains the user define class object? (Note: If Suppose consid..

Answer / 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

More Core Java Interview Questions

Are strings immutable in java?

0 Answers  


What is the size of int in 64-bit jvm?

0 Answers  


Hi, This is ravi i have a question like this i have string "UNDERSTAND" now i want to count the letters how many times it occures.i.e from the above string the out put should be like this U-1,N-2,D-2,E-1,R-1,S-1,T-1,A-1. how can i achieve this Thnaks in advance for your response ..

7 Answers  


What are the advantages of encapsulation in java?

0 Answers  


What are the principle concepts of oops?

0 Answers  






What is the purpose of finalization?

4 Answers  


explain copyonwritearraylist and when do we use copyonwritearraylist?

0 Answers  


What is the mapping mechanism used by java to identify IDL language?

0 Answers  


What do you mean by Hash Map and Hash Table?

0 Answers   Atos Origin,


What is difference between static variable and global variable?

0 Answers  


Is java se free?

0 Answers  


What is the difference between public, private, protected, and friend access?

0 Answers   Amazon,


Categories