I want to sort some objects which are in an ArrayList on the basis of the objects properties.
the object has:
public class Minterm
{
String minTerm;
char flagTick;
String minTermDerive;
int groupNo;
String adjGroup;
static int MaxLiterals;
then i have this in the main method:
ArrayList<Minterm> column =new ArrayList<Minterm>();
then i add some objects of type Minterm in the list. but at the end i want to organise them and sort them on the member variable groupNo(Ascending order).
i searched and came up with the comparable and comparator interfaces that i tried but didnt succeed. is there any other method to do this? or am i doing the comparator implemnting wrong.
EDIT :
Following is the code i wrote for Comparator. Please confirm if it will sort in ascending?
package backEnd;
import java.util.Comparator;
public class Comp implements Comparator<Minterm>
{
@Override
public int compare(Minterm a, Minterm b)
{
return a.getgroupOne().compareTo(b.getgroupOne());
}
}
i run it as:
Collections.sort(column , new Comp());
seems to be working fine. but i dont have a sound understanding of it.
Please confirm if it will sort in ascending?
You should let
MintermimplementComparable<MinTerm>or write a customComparatorforMinTermand then useCollections.sort.Using a comparator it would look like this:
Regarding your edit:
Yes. that sorts
Minterms based on the groups in ascending order.