A simple Approach:
//Declare your Candidate class as:
class Candidate
{
int id;
String name;
Candidate(int id,String name)
{
this.id=id;
this.name=name;
}
public int getId()
{
return this.id;
}
public String getName()
{
return this.Name;
}
}
class Main
{
public static void main(String args[])
{
ArrayList<Candidate> al=new ArrayList<Candidate>();
al.add(new Candidate(1,"ABC"));
al.add(new Candidate(2,"XYZ"));
al.add(new Candidate(3,"PQR"));
Collections.sort(al,new SortByName());
System.out.println(al);
}
}
//Declare a Comparator to sort by Name
class SortByName implements Comparator<Candidate>
{
public int compare(Candidate c1,Candidate c2)
{
return c1.getName().compareTo(c2.getName());
}
}
// As you have posted in your question that you are beginner to java, and as per your requirement you want to have container to store objects of class, and then sort them by thier field name. Hence, I have used ArrayList from Collections class which is very simple to use and learn for beginner's like, I would reckon you to go through some basic concept's of arraylist class,Comparator,Collections method etc.