It is not possible to dynamically create an Agent and THEN adding it to a population. The only way is to use the function that Felipe already put as an answer (add_populationName()) or using the population settings of the Source block.
However: Since your ultimate goal is to sort the Agents, why dont you just use a Collection/List instead? An Agent population is in fact a list as well, just with some AnyLogic specific extras. To dynamically create Agents and sort them, do the following:
- Create your Agent dynamically:
MyAgentType agent1 = new MyAgentType();
- Fill parameter of your agent (if you didnt do it yet during the creation):
agent1.myParameter = 10;
- Add it to an existing Collection of type MyAgentType:
myCollection.add(agent1);
To sort the Agents in the Collection based on multiple parameters, use a custom comparator, as pointed out in this answer:
Collections.sort(myCollection, new Comparator<MyAgentType>(){
public int compare(MyAgentType a1, MyAgentType a2){
if(a1.myParameter == a2.myParameter)
return 0;
return a1.myParameter < a2.myParameter ? -1 : 1;
}
});
In this example just the one parameter is compared, but you can extend this to your needs.