Java beginner Java beginner - 3 months ago 7
Java Question

Indentifier expected error while sorting

I have problems sorting mine object arraylist.

I have created the following class:



public class Gene {

private static String Species = "HomoSapien";
public int GeneId;
private String Symbol, ChromosomeMapLocation;

//set the gene id
public void setGeneId(int ID) {
this.GeneId = ID;
}

//get the gene id
public int getGeneId() {
return this.GeneId;
}

public void setSymbol(String symb) {
this.Symbol = symb;
}

public String getSymbol() {
return this.Symbol;
}

public void setChromosomeMapLocation(String location) {
this.ChromosomeMapLocation = location;
}

public String getChromosomeMapLocation() {
return this.ChromosomeMapLocation;
}
}


I have added 10 Gene objects to an arraylist which I called GeneData. Now I want to sort this GeneData arraylist based on GeneID (which is an integer property).

So I wrote the comparator class:


class GeneIdComparator implements Comparator<Gene> {
@Override
public int compare(Gene a, Gene b) {
return a.GeneId < b.GeneId ? -1 : a.GeneId == b.GeneId ? 0 : 1;
}
Collections.sort(GeneData, new GeneIdComparator());
}


However I get an
<indentifier expected>
message in this line:
Collections.sort(GeneData, new GeneIdComparator());
. I hope someone can give me some suggestions.

Just started Java programming

Answer

remove this from the class GeneIdComparator

 Collections.sort(GeneData, new GeneIdComparator());  
        }

that is not necessary,

place it instead in some method where the GeneData arraylist is declared

because is there where the sort will be done and where the GeneData is used...