Mr. Meeshmew
Mr. Meeshmew

Reputation: 11

I want to alphabetize objects by a variable defined in its class

I am making a program that sets up an experiment for me and I want to alphabetize the subjects (or people) that i input. I have an arraylist of type subjects and i want to alphabetize them by their names.

import java.util.Random;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class Experiment
{
public Random number;
public ArrayList<String> allSubject;
public ArrayList<Subject> allSubjects,alphaSubjects;
public ArrayList<Group> experiment;
public Integer value;
public HashMap<Integer,Subject> matched;
private ArrayList<Integer> numbers;
/**
 * Make a new Experiment.  Then use method addSubject to add
 * Subjects to your experiment.  Then call the assignGroups
 * method to assign Subjects to each group.
 */
public Experiment()
{
    number = new Random();
    numbers = new ArrayList<Integer>();
    experiment = new ArrayList<Group>();
    matched = new HashMap<Integer,Subject>();
    allSubjects = new ArrayList<Subject>(); 
    allSubject = new ArrayList<String>();
    alphaSubjects = new ArrayList<Subject>();
}

/**
 * Alphabetizes the list of Subjects based on their
 * name input by the user.  As of right now, this method
 * is case sensitive meaning Strings starting with 
 * capitals will be listed before those without capitals.
 */
private void alphabetize()
{

           Collections.sort(allSubject);

        //compare the String arraylist to the subject arraylist to reset the subject arraylist indeces in alphabetical order.

       for(int i =0;i<allSubject.size();i++)
       {
        String theName = allSubject.get(i);
         for(Subject subject:allSubjects)
        {
          if(subject.getName().toLowerCase().contains(theName))
         {
            alphaSubjects.add(new Subject(subject.getName(),subject.getDescription()));
         }
        }

     }
}
/**
 * Adds a new Subject to the experiment.
 */
public void addSubject(String name, String description)
{
    allSubjects.add(new Subject(name,description));
    allSubject.add((name.toLowerCase()));
}

So instead of having to add a subject to an arraylist then having to strip the name from that subject and add it to a completely different arraylist, is there a way to alphabetize by the name of the subject.

oh and here is the class: subject.

public class Subject
{
public final String name;
public final String description;
public Subject(String name, String description)
{
    this.name = name;
    this.description = description;
}
public Subject(int aNumber)
{
    name = "Subject" + aNumber;
    aNumber++;
    description = "default";
}
public String getName()
{
    return name;
}
public String getDescription()
{
    return description;
}

}

Upvotes: 0

Views: 285

Answers (3)

Dark Knight
Dark Knight

Reputation: 8357

You can implement Comparator or Comparable and override compare(..) , compareTo(..) method as per your convince. In your case you need to consider 'name of the subject' while implementing this method. Then Collections.sort(yourList<Subject>) will give you sorted result based on subjects name.

    import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class Subject implements Comparable<Subject>
{
public final String name;
public final String description;
public Subject(String name, String description)
{
    this.name = name;
    this.description = description;
}
public Subject(int aNumber)
{
    name = "Subject" + aNumber;
    aNumber++;
    description = "default";
}
public String getName()
{
    return name;
}
public String getDescription()
{
    return description;
}

    @Override
    public int compareTo(Subject o) {
        return this.getName().toUpperCase().compareTo(((Subject)o).getName());
    }

    public static void main(String[] args) {
        Subject s3 = new Subject("C","");
        Subject s1 = new Subject("Z","");
        Subject s2 = new Subject("A","");

        List<Subject> list = new ArrayList<Subject>();
        list.add(s1);
        list.add(s2);
        list.add(s3);

        Collections.sort(list);

        for(Subject sub:list){
            System.out.println(sub.getName());
        }
    }
}

Upvotes: 0

Steve P.
Steve P.

Reputation: 14709

All you need to do is have your class implement Comparable and add a compareTo():

public class Subject implements Comparable<Subject>
{
    ....

     @Override
     public int compareTo(Subject other)
     {
         return this.getName().compareTo(other.getName());
     }
}

Now, since your class implements Comparable of itself, you can use Collections.sort() to sort your list of Subjects.

Here's a tutorial for more info. Good luck!

Upvotes: 0

PKopachevsky
PKopachevsky

Reputation: 260

You can wrap subject ArrayList with SortedList(http://www.glazedlists.com/documentation/tutorial-100#TOC-Sorting-Tables-Sorting-Tables) using your own comparator.

SortedList sortedSubjects = new SortedList<Subject>(allSubjects,new Comparator<Subject>() {
        @Override
        public int compare(Subject left, Subject right) {
            return left.getName().compareTo(right.getName);
        }
    }); 

Upvotes: 1

Related Questions