diff --git a/CHANGELOG b/CHANGELOG
index 1412aa62f84c143aff1beb16b043ab98bd9ac51d..7470a35bba857a34e0550e4366d72126a3f662bd 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -7,6 +7,8 @@ minerva (18.1.0) stable; urgency=medium
   * Bug fix: information about species pathway/compartment has not been
     separated. This caused to export the model into incorrect SBML - species are
     separated using compartment (#2169)
+  * Bug fix: exporting model should provide deterministic output for species and
+    compartments (#2187)
 
  -- Piotr Gawron <piotr.gawron@uni.lu>  Thu, 19 Dec 2024 13:00:00 +0200
 
diff --git a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/CellDesignerXmlParser.java b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/CellDesignerXmlParser.java
index dff6f97af0094312578fb5c09a6d4bf02c6a7b62..d0bceeb60ee61462bcc2f7d95e989360a4834256 100644
--- a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/CellDesignerXmlParser.java
+++ b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/CellDesignerXmlParser.java
@@ -715,7 +715,7 @@ public class CellDesignerXmlParser extends Converter {
     final List<Gene> genes = new ArrayList<>();
     final List<Rna> rnas = new ArrayList<>();
     final List<AntisenseRna> antisenseRnas = new ArrayList<>();
-    for (final Element element : model.getElements()) {
+    for (final Element element : model.getSortedElements()) {
       if (element instanceof Protein) {
         proteins.add((Protein) element);
       } else if (element instanceof Gene) {
diff --git a/converter-SBGNML/src/main/java/lcsb/mapviewer/converter/model/sbgnml/SbgnmlXmlExporter.java b/converter-SBGNML/src/main/java/lcsb/mapviewer/converter/model/sbgnml/SbgnmlXmlExporter.java
index 321f5d8a44ac4e06591d0722efaae71b4fec1682..1aedbdef808958c52e258be2bc1e29f01b78d56d 100644
--- a/converter-SBGNML/src/main/java/lcsb/mapviewer/converter/model/sbgnml/SbgnmlXmlExporter.java
+++ b/converter-SBGNML/src/main/java/lcsb/mapviewer/converter/model/sbgnml/SbgnmlXmlExporter.java
@@ -211,7 +211,7 @@ public class SbgnmlXmlExporter {
 
     final Sbgn sbgnData = new Sbgn();
 
-    for (final Element element : model.getElements()) {
+    for (final Element element : model.getSortedElements()) {
       if (element instanceof Species) {
         if (((Species) element).getComplex() == null) {
           final Glyph newGlyph = elementToGlyph(element);
diff --git a/model/src/main/java/lcsb/mapviewer/model/map/model/ElementByIdComparator.java b/model/src/main/java/lcsb/mapviewer/model/map/model/ElementByIdComparator.java
new file mode 100644
index 0000000000000000000000000000000000000000..a59f3de08839fd0c05a9b11b5ba0cac761d7a702
--- /dev/null
+++ b/model/src/main/java/lcsb/mapviewer/model/map/model/ElementByIdComparator.java
@@ -0,0 +1,18 @@
+package lcsb.mapviewer.model.map.model;
+
+import lcsb.mapviewer.common.comparator.StringComparator;
+import lcsb.mapviewer.model.map.species.Element;
+
+import java.util.Comparator;
+
+public class ElementByIdComparator implements Comparator<Element> {
+  private final StringComparator comparator = new StringComparator();
+
+  @Override
+  public int compare(final Element o1, final Element o2) {
+    if (comparator.compare(o1.getElementId(), o2.getElementId()) == 0) {
+      return o1.getId() - o2.getId();
+    }
+    return comparator.compare(o1.getElementId(), o2.getElementId());
+  }
+}
diff --git a/model/src/main/java/lcsb/mapviewer/model/map/model/Model.java b/model/src/main/java/lcsb/mapviewer/model/map/model/Model.java
index 0a694e9307b033c47adc5a9a39493d008385082a..b9c683193f3b421dbe0a78938610e3115966f65c 100644
--- a/model/src/main/java/lcsb/mapviewer/model/map/model/Model.java
+++ b/model/src/main/java/lcsb/mapviewer/model/map/model/Model.java
@@ -1,13 +1,6 @@
 package lcsb.mapviewer.model.map.model;
 
-import java.util.Calendar;
-import java.util.Collection;
-import java.util.Comparator;
-import java.util.List;
-import java.util.Set;
-
 import com.fasterxml.jackson.annotation.JsonIgnore;
-
 import lcsb.mapviewer.model.Project;
 import lcsb.mapviewer.model.map.BioEntity;
 import lcsb.mapviewer.model.map.Drawable;
@@ -25,13 +18,18 @@ import lcsb.mapviewer.model.map.species.Complex;
 import lcsb.mapviewer.model.map.species.Element;
 import lcsb.mapviewer.model.map.species.Species;
 
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Set;
+
 /**
  * This interface defines functionality that the model container class should
  * implement to access data efficiently. It shouldn't be implemented by data
  * model.
- * 
+ *
  * @author Piotr Gawron
- * 
  */
 public interface Model {
 
@@ -45,45 +43,38 @@ public interface Model {
 
   /**
    * Adds element to the model.
-   * 
-   * @param element
-   *          element to add
+   *
+   * @param element element to add
    */
   void addElement(final Element element);
 
   /**
-   * 
    * @return model width
    */
   double getWidth();
 
   /**
    * Sets model width.
-   * 
-   * @param width
-   *          new model width
+   *
+   * @param width new model width
    */
   void setWidth(final double width);
 
   /**
    * Sets model width.
    *
-   * @param text
-   *          new model width
+   * @param text new model width
    */
   @JsonIgnore
   void setWidth(final String text);
 
   /**
-   *
-   * @param width
-   *          new {@link ModelData#width}
+   * @param width new {@link ModelData#width}
    */
   @JsonIgnore
   void setWidth(final int width);
 
   /**
-   *
    * @return model height
    */
   double getHeight();
@@ -91,24 +82,20 @@ public interface Model {
   /**
    * Sets model height.
    *
-   * @param height
-   *          new model height
+   * @param height new model height
    */
   void setHeight(final double height);
 
   /**
    * Sets model height.
    *
-   * @param text
-   *          new model height
+   * @param text new model height
    */
   @JsonIgnore
   void setHeight(final String text);
 
   /**
-   *
-   * @param height
-   *          new {@link ModelData#height}
+   * @param height new {@link ModelData#height}
    */
   @JsonIgnore
   void setHeight(final int height);
@@ -121,9 +108,7 @@ public interface Model {
   Set<Element> getElements();
 
   /**
-   *
-   * @param elements
-   *          new {@link ModelData#elements} collection
+   * @param elements new {@link ModelData#elements} collection
    */
   void setElements(final Set<Element> elements);
 
@@ -131,10 +116,8 @@ public interface Model {
    * Returns element with the given element identifier
    * ({@link Element#elementId} ).
    *
-   * @param idElement
-   *          element identifier
-   * @param <T>
-   *          type of the object to be returned
+   * @param idElement element identifier
+   * @param <T>       type of the object to be returned
    * @return {@link Element} with the given id
    */
   <T extends Element> T getElementByElementId(final String idElement);
@@ -142,8 +125,7 @@ public interface Model {
   /**
    * Adds reaction to the model.
    *
-   * @param reaction
-   *          reaction to add
+   * @param reaction reaction to add
    */
   void addReaction(final Reaction reaction);
 
@@ -162,13 +144,11 @@ public interface Model {
   /**
    * Adds layer to the model.
    *
-   * @param layer
-   *          object to add
+   * @param layer object to add
    */
   void addLayer(final Layer layer);
 
   /**
-   *
    * @return set of layers
    */
   Set<Layer> getLayers();
@@ -176,13 +156,11 @@ public interface Model {
   /**
    * Adds list of elements into model.
    *
-   * @param elements
-   *          list of elements
+   * @param elements list of elements
    */
   void addElements(final Collection<? extends Element> elements);
 
   /**
-   *
    * @return short description of the model
    */
   String getNotes();
@@ -190,16 +168,14 @@ public interface Model {
   /**
    * Sets new short description of the model.
    *
-   * @param notes
-   *          new short description
+   * @param notes new short description
    */
   void setNotes(final String notes);
 
   /**
    * Returns reaction with the id given in the parameter.
    *
-   * @param idReaction
-   *          reaction identifier ({@link Reaction#idReaction})
+   * @param idReaction reaction identifier ({@link Reaction#idReaction})
    * @return reaction with the id given in the parameter
    */
   Reaction getReactionByReactionId(final String idReaction);
@@ -207,24 +183,21 @@ public interface Model {
   /**
    * Adds set of layers to the model.
    *
-   * @param layers
-   *          object to add
+   * @param layers object to add
    */
   void addLayers(final Collection<Layer> layers);
 
   /**
    * Adds {@link ElementGroup} to the model.
    *
-   * @param elementGroup
-   *          object to add
+   * @param elementGroup object to add
    */
   void addElementGroup(final ElementGroup elementGroup);
 
   /**
    * Adds {@link BlockDiagram} to the model.
    *
-   * @param blockDiagram
-   *          object to add
+   * @param blockDiagram object to add
    */
   void addBlockDiagream(final BlockDiagram blockDiagram);
 
@@ -235,8 +208,7 @@ public interface Model {
   String getIdModel();
 
   /**
-   * @param idModel
-   *          the idModel to set
+   * @param idModel the idModel to set
    * @see Model#idModel
    */
   void setIdModel(final String idModel);
@@ -248,8 +220,7 @@ public interface Model {
   int getTileSize();
 
   /**
-   * @param tileSize
-   *          the tileSize to set
+   * @param tileSize the tileSize to set
    * @see ModelData#tileSize
    */
   void setTileSize(final int tileSize);
@@ -261,8 +232,7 @@ public interface Model {
   int getZoomLevels();
 
   /**
-   * @param zoomLevels
-   *          the zoomLevels to set
+   * @param zoomLevels the zoomLevels to set
    * @see ModelData#zoomLevels
    */
   void setZoomLevels(final int zoomLevels);
@@ -270,16 +240,14 @@ public interface Model {
   /**
    * Removes reaction from model.
    *
-   * @param reaction
-   *          reaction to remove
+   * @param reaction reaction to remove
    */
   void removeReaction(final Reaction reaction);
 
   /**
    * Removes {@link Element} from the model.
    *
-   * @param element
-   *          element to remove
+   * @param element element to remove
    */
   void removeElement(final Element element);
 
@@ -307,16 +275,14 @@ public interface Model {
   /**
    * Adds reactions to model.
    *
-   * @param reactions2
-   *          list of reaction to add
+   * @param reactions2 list of reaction to add
    */
   void addReactions(final List<Reaction> reactions2);
 
   /**
    * Returns list of elements with given name.
    *
-   * @param name
-   *          name of the element
+   * @param name name of the element
    * @return list of elements with given name
    */
   List<Element> getElementsByName(final String name);
@@ -324,8 +290,7 @@ public interface Model {
   /**
    * Returns {@link Element} for given database identifier.
    *
-   * @param dbId
-   *          element database identifier ({@link Element#id})
+   * @param dbId element database identifier ({@link Element#id})
    * @return {@link Element} for a given id
    */
   <T extends Element> T getElementByDbId(final Integer dbId);
@@ -333,8 +298,7 @@ public interface Model {
   /**
    * Returns {@link Reaction} for given database identifier.
    *
-   * @param dbId
-   *          reaction database identifier ({@link Reaction#id})
+   * @param dbId reaction database identifier ({@link Reaction#id})
    * @return {@link Reaction} for a given id
    */
   Reaction getReactionByDbId(final Integer dbId);
@@ -354,15 +318,12 @@ public interface Model {
   List<Element> getElementsSortedBySize();
 
   /**
-   * 
    * @return {@link ModelData#project}
    */
   Project getProject();
 
   /**
-   *
-   * @param project
-   *          new {@link ModelData#project}
+   * @param project new {@link ModelData#project}
    */
   void setProject(final Project project);
 
@@ -372,7 +333,6 @@ public interface Model {
   ModelData getModelData();
 
   /**
-   * 
    * @return {@link ModelData#id}
    */
   int getId();
@@ -380,16 +340,14 @@ public interface Model {
   /**
    * Sets database identifier of the model.
    *
-   * @param id
-   *          database identifier
+   * @param id database identifier
    */
   void setId(final int id);
 
   /**
    * Adds submodel connection.
    *
-   * @param submodel
-   *          submodel to add
+   * @param submodel submodel to add
    */
   void addSubmodelConnection(final ModelSubmodelConnection submodel);
 
@@ -410,8 +368,7 @@ public interface Model {
   /**
    * Sets name of the model.
    *
-   * @param name
-   *          name of the model
+   * @param name name of the model
    */
   void setName(final String name);
 
@@ -419,19 +376,17 @@ public interface Model {
    * Returns {@link Model submodel} by the {@link ModelData#id database
    * identifier} given in the parameter.
    *
-   * @param idObject
-   *          the {@link ModelData#id database identifier} that identifies
-   *          submodel
+   * @param idObject the {@link ModelData#id database identifier} that identifies
+   *                 submodel
    * @return {@link Model submodel} by the {@link ModelData#id database
-   *         identifier} given in the parameter
+   * identifier} given in the parameter
    */
   Model getSubmodelById(final Integer idObject);
 
   /**
    * Returns submodel identified by submodel identifier.
    *
-   * @param identifier
-   *          identifier of the model
+   * @param identifier identifier of the model
    * @return submodel identified by identifier
    */
   Model getSubmodelById(final String identifier);
@@ -447,8 +402,7 @@ public interface Model {
   /**
    * Returns connection to a submodel identified by connection name.
    *
-   * @param name
-   *          name of the connection
+   * @param name name of the connection
    * @return connection to a submodel identified by connection name
    */
   Model getSubmodelByConnectionName(final String name);
@@ -456,8 +410,7 @@ public interface Model {
   /**
    * Returns connection to a submodel identified by connection identifier.
    *
-   * @param id
-   *          id of the connection
+   * @param id id of the connection
    * @return connection to a submodel identified by connection identifier
    */
   SubmodelConnection getSubmodelConnectionById(final Integer id);
@@ -473,17 +426,16 @@ public interface Model {
    * Returns {@link Model submodel} identified by the {@link ModelData#name
    * model name}. It returns this 'parent' object when the names matches.
    *
-   * @param name
-   *          name of the submodel that should be returned
+   * @param name name of the submodel that should be returned
    * @return {@link Model submodel} identified by the {@link ModelData#name
-   *         model name}
+   * model name}
    */
   Model getSubmodelByName(final String name);
 
   /**
    * Return list of all {@link BioEntity} in the map. This includes all
    * {@link Reaction reactions} and {@link Element elements}.
-   * 
+   *
    * @return list of all {@link BioEntity} in the map
    */
   List<BioEntity> getBioEntities();
@@ -532,25 +484,27 @@ public interface Model {
 
   void addMiriamData(final Collection<MiriamData> parseRdfNode);
 
-  public void addAuthor(final Author author);
+  void addAuthor(final Author author);
 
   void addAuthors(final Collection<Author> authorsFromRdf);
 
-  public List<Author> getAuthors();
+  List<Author> getAuthors();
 
-  public Calendar getCreationDate();
+  Calendar getCreationDate();
 
-  public void setCreationDate(final Calendar creationDate);
+  void setCreationDate(final Calendar creationDate);
 
-  public void addModificationDate(final Calendar modificationDate);
+  void addModificationDate(final Calendar modificationDate);
 
-  public List<Calendar> getModificationDates();
+  List<Calendar> getModificationDates();
 
   void addModificationDates(final Collection<Calendar> modificationDatesFromRdf);
 
-  void removeBioEntity(final BioEntity bioEntitiy);
+  void removeBioEntity(final BioEntity bioEntity);
 
   Set<Drawable> getDrawables();
 
   Set<Drawable> getDrawables(boolean onlyVisible);
+
+  Collection<Element> getSortedElements();
 }
diff --git a/model/src/main/java/lcsb/mapviewer/model/map/model/ModelFullIndexed.java b/model/src/main/java/lcsb/mapviewer/model/map/model/ModelFullIndexed.java
index 2301de3071c32efbab8e4bdab8b44a885708fb5b..ffa13ff0832c8ee05f30e028ec815bcff95b131e 100644
--- a/model/src/main/java/lcsb/mapviewer/model/map/model/ModelFullIndexed.java
+++ b/model/src/main/java/lcsb/mapviewer/model/map/model/ModelFullIndexed.java
@@ -1,18 +1,5 @@
 package lcsb.mapviewer.model.map.model;
 
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
-
 import lcsb.mapviewer.common.exception.InvalidArgumentException;
 import lcsb.mapviewer.model.Project;
 import lcsb.mapviewer.model.map.BioEntity;
@@ -30,14 +17,25 @@ import lcsb.mapviewer.model.map.species.Complex;
 import lcsb.mapviewer.model.map.species.Element;
 import lcsb.mapviewer.model.map.species.Species;
 import lcsb.mapviewer.model.map.species.field.SpeciesWithModificationResidue;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 /**
  * This class implements {@link Model} interface. It's is very simple
  * implementation containing structures that index the data from
  * {@link ModelData} structure and provide access method to this indexed data.
- * 
+ *
  * @author Piotr Gawron
- * 
  */
 public class ModelFullIndexed implements Model {
 
@@ -45,31 +43,31 @@ public class ModelFullIndexed implements Model {
    * Default class logger.
    */
   @SuppressWarnings("unused")
-  private static Logger logger = LogManager.getLogger();
+  private static final Logger logger = LogManager.getLogger();
 
   /**
    * Object that map {@link Element#elementId element identifier} into
    * {@link Element}.
    */
-  private Map<String, Element> elementByElementId = new HashMap<>();
+  private final Map<String, Element> elementByElementId = new HashMap<>();
 
   /**
    * Object that map {@link Element#id element database identifier} into
    * {@link Element}.
    */
-  private Map<Integer, Element> elementByDbId = new HashMap<>();
+  private final Map<Integer, Element> elementByDbId = new HashMap<>();
 
   /**
    * Object that map {@link Reaction#idReaction reaction identifier} into
    * {@link Reaction}.
    */
-  private Map<String, Reaction> reactionByReactionId = new HashMap<>();
+  private final Map<String, Reaction> reactionByReactionId = new HashMap<>();
 
   /**
    * Object that map {@link Reaction#id reaction database identifier} into
    * {@link Reaction}.
    */
-  private Map<Integer, Reaction> reactionByDbId = new HashMap<>();
+  private final Map<Integer, Reaction> reactionByDbId = new HashMap<>();
 
   /**
    * {@link ModelData} object containing "raw" data about the model.
@@ -78,9 +76,8 @@ public class ModelFullIndexed implements Model {
 
   /**
    * Default constructor.
-   * 
-   * @param model
-   *          {@link ModelData} object containing "raw" data about the model
+   *
+   * @param model {@link ModelData} object containing "raw" data about the model
    */
   public ModelFullIndexed(final ModelData model) {
     if (model == null) {
@@ -202,6 +199,7 @@ public class ModelFullIndexed implements Model {
         result.add((Compartment) element);
       }
     }
+    result.sort(new ElementByIdComparator());
     return result;
   }
 
@@ -322,6 +320,7 @@ public class ModelFullIndexed implements Model {
         result.add((Species) element);
       }
     }
+    result.sort(new ElementByIdComparator());
     return result;
   }
 
@@ -333,6 +332,7 @@ public class ModelFullIndexed implements Model {
         result.add((Species) element);
       }
     }
+    result.sort(new ElementByIdComparator());
     return result;
   }
 
@@ -344,6 +344,7 @@ public class ModelFullIndexed implements Model {
         result.add((Complex) element);
       }
     }
+    result.sort(new ElementByIdComparator());
     return result;
   }
 
@@ -700,13 +701,13 @@ public class ModelFullIndexed implements Model {
   }
 
   @Override
-  public void removeBioEntity(final BioEntity bioEntitiy) {
-    if (bioEntitiy instanceof Reaction) {
-      removeReaction((Reaction) bioEntitiy);
-    } else if (bioEntitiy instanceof Element) {
-      removeElement((Element) bioEntitiy);
+  public void removeBioEntity(final BioEntity bioEntity) {
+    if (bioEntity instanceof Reaction) {
+      removeReaction((Reaction) bioEntity);
+    } else if (bioEntity instanceof Element) {
+      removeElement((Element) bioEntity);
     } else {
-      throw new InvalidArgumentException("Unknown class type:" + bioEntitiy);
+      throw new InvalidArgumentException("Unknown class type:" + bioEntity);
     }
   }
 
@@ -731,4 +732,11 @@ public class ModelFullIndexed implements Model {
     }
     return result;
   }
+
+  @Override
+  public Collection<Element> getSortedElements() {
+    List<Element> result = new ArrayList<>(getElements());
+    result.sort(new ElementByIdComparator());
+    return result;
+  }
 }
diff --git a/model/src/test/java/lcsb/mapviewer/model/map/model/ModelFullIndexedTest.java b/model/src/test/java/lcsb/mapviewer/model/map/model/ModelFullIndexedTest.java
index c2fffd96d1f5f1f0deff454fca5711a2bf2f91aa..e868f83a9385a5a1d23a5975a5a5122eade8be26 100644
--- a/model/src/test/java/lcsb/mapviewer/model/map/model/ModelFullIndexedTest.java
+++ b/model/src/test/java/lcsb/mapviewer/model/map/model/ModelFullIndexedTest.java
@@ -1,21 +1,5 @@
 package lcsb.mapviewer.model.map.model;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.Mockito;
-
 import lcsb.mapviewer.ModelTestFunctions;
 import lcsb.mapviewer.common.Configuration;
 import lcsb.mapviewer.common.exception.InvalidArgumentException;
@@ -36,6 +20,21 @@ import lcsb.mapviewer.model.map.species.GenericProtein;
 import lcsb.mapviewer.model.map.species.Protein;
 import lcsb.mapviewer.model.map.species.Species;
 import lcsb.mapviewer.model.map.species.field.StructuralState;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 
 public class ModelFullIndexedTest extends ModelTestFunctions {
 
@@ -543,4 +542,24 @@ public class ModelFullIndexedTest extends ModelTestFunctions {
     assertTrue(model.getDrawables().contains(state));
   }
 
+  @Test
+  public void testGetSortedElements() {
+    ModelFullIndexed model = new ModelFullIndexed(null);
+
+    Protein protein1 = new GenericProtein("1");
+    Protein protein2 = new GenericProtein("3");
+    Protein protein3 = new GenericProtein("2");
+    Protein protein4 = new GenericProtein("4");
+
+    model.addElement(protein1);
+
+    assertEquals(protein1, model.getSortedElements().iterator().next());
+    model.addElement(protein2);
+    assertEquals(protein1, model.getSortedElements().iterator().next());
+    model.addElement(protein3);
+    assertEquals(protein1, model.getSortedElements().iterator().next());
+    model.addElement(protein4);
+    assertEquals(protein1, model.getSortedElements().iterator().next());
+  }
+
 }
diff --git a/pathvisio/src/main/java/lcsb/mapviewer/wikipathway/xml/ModelToGPML.java b/pathvisio/src/main/java/lcsb/mapviewer/wikipathway/xml/ModelToGPML.java
index 33366ea4f4a148ffb604c598c5de7b9fa98d6a79..7efeeb482288e4cb08877a062b244c6a84e65e50 100644
--- a/pathvisio/src/main/java/lcsb/mapviewer/wikipathway/xml/ModelToGPML.java
+++ b/pathvisio/src/main/java/lcsb/mapviewer/wikipathway/xml/ModelToGPML.java
@@ -1,23 +1,5 @@
 package lcsb.mapviewer.wikipathway.xml;
 
-import java.awt.Color;
-import java.awt.Polygon;
-import java.awt.Shape;
-import java.awt.geom.Ellipse2D;
-import java.awt.geom.Line2D;
-import java.awt.geom.Point2D;
-import java.awt.geom.Rectangle2D;
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
-import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.text.StringEscapeUtils;
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
-
 import lcsb.mapviewer.common.XmlParser;
 import lcsb.mapviewer.common.exception.InvalidArgumentException;
 import lcsb.mapviewer.common.geometry.ColorParser;
@@ -60,12 +42,28 @@ import lcsb.mapviewer.wikipathway.model.GpmlLineType;
 import lcsb.mapviewer.wikipathway.model.InteractionMapping;
 import lcsb.mapviewer.wikipathway.model.ShapeMapping;
 import lcsb.mapviewer.wikipathway.utils.Geo;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.text.StringEscapeUtils;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.awt.Color;
+import java.awt.Polygon;
+import java.awt.Shape;
+import java.awt.geom.Ellipse2D;
+import java.awt.geom.Line2D;
+import java.awt.geom.Point2D;
+import java.awt.geom.Rectangle2D;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
 
 /**
  * Class contains methods for ModelToGPML conversion.
- * 
+ *
  * @author Jan Badura
- * 
  */
 public class ModelToGPML {
 
@@ -104,15 +102,15 @@ public class ModelToGPML {
   /**
    * Parser used to convert from/to xml {@link MiriamData} annotations.
    */
-  private ReferenceParser referenceParser;
+  private final ReferenceParser referenceParser;
 
   /**
    * Parser used to convert from/to xml (in BioPAX format) {@link MiriamData}
    * annotations.
    */
-  private BiopaxParser biopaxParser;
+  private final BiopaxParser biopaxParser;
 
-  private ColorParser colorParser = new ColorParser();
+  private final ColorParser colorParser = new ColorParser();
 
   public ModelToGPML(final String mapName) {
     referenceParser = new ReferenceParser(mapName);
@@ -121,7 +119,7 @@ public class ModelToGPML {
 
   /**
    * Returns new unique identifier for the model.
-   * 
+   *
    * @return new unique identifier for the model
    */
   private String getNewId() {
@@ -133,9 +131,8 @@ public class ModelToGPML {
    * This function returns GPML Species type based on class. If it can not
    * recognize one of (Rna,GeneProduct,Protein,Metabolite) it will throw
    * {@link InvalidArgumentException}.
-   * 
-   * @param species
-   *          species for which GPML type will be returned
+   *
+   * @param species species for which GPML type will be returned
    * @return String - type
    */
   private String getType(final Species species) {
@@ -171,9 +168,8 @@ public class ModelToGPML {
    * Since in pathvisio it is impossible to set the size of group rectangle this
    * function calculates new rectangle for complex based on contents of this
    * complex.
-   * 
-   * @param alias
-   *          alias for which rectangle border is calculated
+   *
+   * @param alias alias for which rectangle border is calculated
    * @return rectangle border for alias
    */
   Shape getShape(final Element alias) {
@@ -237,11 +233,9 @@ public class ModelToGPML {
    * This function calculates {@link PolylineData} based on start {@link Reactant}
    * and end {@link Product}. If end or start points are to far away from their
    * rectangles new points are added.
-   * 
-   * @param start
-   *          reactant from which polyline starts
-   * @param end
-   *          product where polyline ends
+   *
+   * @param start reactant from which polyline starts
+   * @param end   product where polyline ends
    * @return {@link PolylineData} between start and end
    */
   private PolylineData getPolyline(final Reactant start, final Product end) {
@@ -280,11 +274,9 @@ public class ModelToGPML {
 
   /**
    * This function calculates PolylineData based on ReactionNode.
-   * 
-   * @param rn
-   *          reaction node for which polyline is calculated
-   * @param mainLine
-   *          main line to which polyline is attached
+   *
+   * @param rn       reaction node for which polyline is calculated
+   * @param mainLine main line to which polyline is attached
    * @return polyline for {@link ReactionNode}
    */
   PolylineData getPolyline(final ReactionNode rn, final PolylineData mainLine) {
@@ -328,13 +320,12 @@ public class ModelToGPML {
   /**
    * This function transform Compartments into Shapes (final Oval or Rectangle)
    * from PathVisio.
-   * 
-   * @param model
-   *          model where compartments are placed
+   *
+   * @param model model where compartments are placed
    * @return String that encodes all compartments in GPML format
    */
   private String getComparments(final Model model) {
-    StringBuilder result = new StringBuilder("");
+    StringBuilder result = new StringBuilder();
     for (final Compartment compartment : model.getCompartments()) {
       result.append(compartmentToXml(compartment));
     }
@@ -342,7 +333,7 @@ public class ModelToGPML {
   }
 
   String compartmentToXml(final Compartment ca) {
-    StringBuilder comparments = new StringBuilder("");
+    StringBuilder comparments = new StringBuilder();
     double x = ca.getCenterX();
     double y = ca.getCenterY();
     double h = ca.getHeight();
@@ -374,23 +365,18 @@ public class ModelToGPML {
    * This function creates Interaction for other product, reactant or modifier
    * from main reaction. This is support function for getInteractions(Model
    * model).
-   * 
-   * @param rn
-   *          object representing reactant/product/modifier
-   * @param anchId
-   *          identifier of the anchor where it will be connected
-   * @param anchors
-   *          string builder where anchors are stored (it will be modified to add
-   *          anchor for this element)
-   * @param mainLine
-   *          line used for connecting this element
+   *
+   * @param rn       object representing reactant/product/modifier
+   * @param anchId   identifier of the anchor where it will be connected
+   * @param anchors  string builder where anchors are stored (it will be modified to add
+   *                 anchor for this element)
+   * @param mainLine line used for connecting this element
    * @return string representing connection of this element to reaction
-   * @throws ConverterException
-   *           thrown when there is a problem with conversion
+   * @throws ConverterException thrown when there is a problem with conversion
    */
   private String getInteractionForAnchor(final ReactionNode rn, final String anchId, final StringBuilder anchors, final PolylineData mainLine)
       throws ConverterException {
-    StringBuilder interaction = new StringBuilder("");
+    StringBuilder interaction = new StringBuilder();
 
     PolylineData line = getPolyline(rn, mainLine);
     Point2D ps = line.getStartPoint();
@@ -486,16 +472,14 @@ public class ModelToGPML {
   /**
    * This function encode Species into DataNodes from GPML format. Empty complexes
    * are also transformed into DataNodes.
-   * 
-   * @param model
-   *          model where aliases are placed
+   *
+   * @param model model where aliases are placed
    * @return string representing {@link lcsb.mapviewer.wikipathway.model.DataNode
-   *         data nodes}
-   * @throws ConverterException
-   *           thrown when there is a problem with conversion
+   * data nodes}
+   * @throws ConverterException thrown when there is a problem with conversion
    */
   protected String getDataNodes(final Model model) throws ConverterException {
-    StringBuilder dataNodes = new StringBuilder("");
+    StringBuilder dataNodes = new StringBuilder();
 
     for (final Species species : model.getNotComplexSpeciesList()) {
       if (!(species instanceof Complex) && !(species instanceof Degraded)) {
@@ -657,13 +641,12 @@ public class ModelToGPML {
 
   /**
    * This function encode ComplexAliases into Groups from GPML format.
-   * 
-   * @param model
-   *          model where aliases are placed
+   *
+   * @param model model where aliases are placed
    * @return string representing groups in GPML format
    */
   protected String getGroups(final Model model) {
-    StringBuilder groups = new StringBuilder("");
+    StringBuilder groups = new StringBuilder();
 
     for (final Complex complex : model.getComplexList()) {
       if (complex.getElements().size() > 0) {
@@ -685,15 +668,13 @@ public class ModelToGPML {
 
   /**
    * This function encode Reactions into Interactions from GPML format.
-   * 
-   * @param model
-   *          model where reactions are placed
+   *
+   * @param model model where reactions are placed
    * @return string with interactions in GPML format
-   * @throws ConverterException
-   *           thrown when there is a problem with conversion
+   * @throws ConverterException thrown when there is a problem with conversion
    */
   protected String getInteractions(final Model model) throws ConverterException {
-    StringBuilder interactions = new StringBuilder("");
+    StringBuilder interactions = new StringBuilder();
 
     for (final Reaction reaction : model.getReactions()) {
       String reactionArrowHead = InteractionMapping.getGpmlInteractionTypeForMinervaReactionClass(reaction.getClass())
@@ -701,8 +682,8 @@ public class ModelToGPML {
 
       String lineStyle = GpmlLineType.getByLineType(reaction.getLine().getType()).getGpmlString();
 
-      StringBuilder anchors = new StringBuilder("");
-      StringBuilder tmp = new StringBuilder("");
+      StringBuilder anchors = new StringBuilder();
+      StringBuilder tmp = new StringBuilder();
 
       interactions.append("  <Interaction GraphId=\"" + reaction.getIdReaction() + "\">\n");
       if (reaction.getNotes() != null) {
@@ -767,12 +748,12 @@ public class ModelToGPML {
         tmp.append(getInteractionForAnchor(modifier, getNewId(), anchors, line));
       }
 
-      interactions.append(anchors.toString());
+      interactions.append(anchors);
       interactions.append("    </Graphics>\n");
       interactions.append(
           referenceParser.toXml(reaction.getMiriamData(), new LogMarker(ProjectLogEntryType.EXPORT_ISSUE, reaction)));
       interactions.append("  </Interaction>\n");
-      interactions.append(tmp.toString());
+      interactions.append(tmp);
     }
 
     return interactions.toString();
@@ -780,15 +761,13 @@ public class ModelToGPML {
 
   /**
    * This function returns Model in GPML format.
-   * 
-   * @param model
-   *          model to transform
+   *
+   * @param model model to transform
    * @return string in GPML format representing model
-   * @throws ConverterException
-   *           thrown when there is a problem with conversion
+   * @throws ConverterException thrown when there is a problem with conversion
    */
   public String getGPML(final Model model) throws ConverterException {
-    StringBuilder gpml = new StringBuilder("");
+    StringBuilder gpml = new StringBuilder();
     gpml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
     gpml.append("<Pathway xmlns=\"http://pathvisio.org/GPML/2013a\"");
     if (model.getName() != null) {
@@ -845,7 +824,7 @@ public class ModelToGPML {
 
   private String getStates(final Model model) throws ConverterException {
     StringBuilder result = new StringBuilder();
-    for (final Element element : model.getElements()) {
+    for (final Element element : model.getSortedElements()) {
       if (element instanceof SpeciesWithModificationResidue) {
         for (final ModificationResidue mr : ((SpeciesWithModificationResidue) element).getModificationResidues()) {
           result.append(modifictionResidueToState(mr));
@@ -860,7 +839,7 @@ public class ModelToGPML {
   }
 
   protected String getShapes(final Model model) throws ConverterException {
-    StringBuilder result = new StringBuilder("");
+    StringBuilder result = new StringBuilder();
     result.append(getComparments(model));
 
     for (final Species species : model.getNotComplexSpeciesList()) {