Index: trunk/src/org/openstreetmap/josm/data/APIDataSet.java
===================================================================
--- trunk/src/org/openstreetmap/josm/data/APIDataSet.java	(revision 4112)
+++ trunk/src/org/openstreetmap/josm/data/APIDataSet.java	(revision 4113)
@@ -20,4 +20,5 @@
 import org.openstreetmap.josm.data.osm.Node;
 import org.openstreetmap.josm.data.osm.OsmPrimitive;
+import org.openstreetmap.josm.data.osm.OsmPrimitiveComparator;
 import org.openstreetmap.josm.data.osm.PrimitiveId;
 import org.openstreetmap.josm.data.osm.Relation;
@@ -70,96 +71,11 @@
             }
         }
-        sortDeleted();
-        sortNew();
-        sortUpdated();
-    }
-
-    /**
-     * Ensures that primitives are deleted in the following order: Relations, then Ways,
-     * then Nodes.
-     *
-     */
-    protected void sortDeleted() {
-        Collections.sort(
-                toDelete,
-                new Comparator<OsmPrimitive>() {
-                    public int compare(OsmPrimitive o1, OsmPrimitive o2) {
-                        if (o1 instanceof Node && o2 instanceof Node)
-                            return 0;
-                        else if (o1 instanceof Node)
-                            return 1;
-                        else if (o2 instanceof Node)
-                            return -1;
-
-                        if (o1 instanceof Way && o2 instanceof Way)
-                            return 0;
-                        else if (o1 instanceof Way && o2 instanceof Relation)
-                            return 1;
-                        else if (o2 instanceof Way && o1 instanceof Relation)
-                            return -1;
-
-                        return 0;
-                    }
-                }
-        );
-    }
-
-    /**
-     * Ensures that primitives are added in the following order: Nodes, then Ways,
-     * then Relations.
-     *
-     */
-    protected void sortNew() {
-        Collections.sort(
-                toAdd,
-                new Comparator<OsmPrimitive>() {
-                    public int compare(OsmPrimitive o1, OsmPrimitive o2) {
-                        if (o1 instanceof Node && o2 instanceof Node)
-                            return 0;
-                        else if (o1 instanceof Node)
-                            return -1;
-                        else if (o2 instanceof Node)
-                            return 1;
-
-                        if (o1 instanceof Way && o2 instanceof Way)
-                            return 0;
-                        else if (o1 instanceof Way && o2 instanceof Relation)
-                            return -1;
-                        else if (o2 instanceof Way && o1 instanceof Relation)
-                            return 1;
-
-                        return 0;
-                    }
-                }
-        );
-    }
-    /* 
-     * Sort list of updated elements, so it looks neat in the confirmation dialog.
-     */
-    protected void sortUpdated() {
-        Collections.sort(
-                toUpdate,
-                new Comparator<OsmPrimitive>() {
-                    public int compare(OsmPrimitive o1, OsmPrimitive o2) {
-                        if (o1 instanceof Node && o2 instanceof Node)
-                            return 0;
-                        else if (o1 instanceof Node)
-                            return -1;
-                        else if (o2 instanceof Node)
-                            return 1;
-
-                        if (o1 instanceof Way && o2 instanceof Way)
-                            return 0;
-                        else if (o1 instanceof Way && o2 instanceof Relation)
-                            return -1;
-                        else if (o2 instanceof Way && o1 instanceof Relation)
-                            return 1;
-
-                        return 0;
-                    }
-                }
-        );
-    }
-    
+        OsmPrimitiveComparator c = new OsmPrimitiveComparator();
+        c.relationsFirst = true;
+        Collections.sort(toDelete, c); 
+        Collections.sort(toAdd, c); 
+        Collections.sort(toUpdate, c); 
+    }
+
     /**
      * initializes the API data set with the modified primitives in <code>ds</code>
@@ -236,6 +152,9 @@
             }
         }
-        sortNew();
-        sortDeleted();
+        OsmPrimitiveComparator c = new OsmPrimitiveComparator();
+        c.relationsFirst = true;
+        Collections.sort(toDelete, c); 
+        Collections.sort(toAdd, c); 
+        Collections.sort(toUpdate, c); 
     }
 
Index: trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitiveComparator.java
===================================================================
--- trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitiveComparator.java	(revision 4113)
+++ trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitiveComparator.java	(revision 4113)
@@ -0,0 +1,62 @@
+// License: GPL. Copyright 2007 by Immanuel Scholz and others
+package org.openstreetmap.josm.data.osm;
+
+import java.util.Comparator;
+import java.util.HashMap;
+
+import org.openstreetmap.josm.gui.DefaultNameFormatter;
+
+/** Comparator, comparing by type and objects display names */
+public class OsmPrimitiveComparator implements Comparator<OsmPrimitive> {
+    final private HashMap<OsmPrimitive, String> cache= new HashMap<OsmPrimitive, String>();
+    final private DefaultNameFormatter df = DefaultNameFormatter.getInstance();
+    public boolean relationsFirst = false;
+
+    private String cachedName(OsmPrimitive p) {
+        String name = cache.get(p);
+        if (name == null) {
+            name = p.getDisplayName(df);
+            cache.put(p, name);
+        }
+        return name;
+    }
+
+    private int compareName(OsmPrimitive a, OsmPrimitive b) {
+        String an = cachedName(a);
+        String bn = cachedName(b);
+        // make sure display names starting with digits are the end of the
+        // list
+        if (Character.isDigit(an.charAt(0)) && Character.isDigit(bn.charAt(0)))
+            return an.compareTo(bn);
+        else if (Character.isDigit(an.charAt(0)) && !Character.isDigit(bn.charAt(0)))
+            return 1;
+        else if (!Character.isDigit(an.charAt(0)) && Character.isDigit(bn.charAt(0)))
+            return -1;
+        return an.compareTo(bn);
+    }
+
+    private int compareType(OsmPrimitive a, OsmPrimitive b) {
+        if(relationsFirst) {
+            // show relations before ways, then nodes
+            if (a.getType().equals(OsmPrimitiveType.RELATION)) return -1;
+            if (a.getType().equals(OsmPrimitiveType.NODE)) return 1;
+            // a is a way
+            if (b.getType().equals(OsmPrimitiveType.RELATION)) return 1;
+            // b is a node
+        } else {
+            // show ways before relations, then nodes
+            if (a.getType().equals(OsmPrimitiveType.WAY)) return -1;
+            if (a.getType().equals(OsmPrimitiveType.NODE)) return 1;
+            // a is a relation
+            if (b.getType().equals(OsmPrimitiveType.WAY)) return 1;
+            // b is a node
+        }
+        return -1;
+    }
+
+    public int compare(OsmPrimitive a, OsmPrimitive b) {
+        if (a.getType().equals(b.getType()))
+            return compareName(a, b);
+        return compareType(a, b);
+    }
+}
Index: trunk/src/org/openstreetmap/josm/gui/dialogs/SelectionListDialog.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/dialogs/SelectionListDialog.java	(revision 4112)
+++ trunk/src/org/openstreetmap/josm/gui/dialogs/SelectionListDialog.java	(revision 4113)
@@ -51,4 +51,5 @@
 import org.openstreetmap.josm.data.osm.Node;
 import org.openstreetmap.josm.data.osm.OsmPrimitive;
+import org.openstreetmap.josm.data.osm.OsmPrimitiveComparator;
 import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
 import org.openstreetmap.josm.data.osm.Relation;
@@ -916,30 +917,13 @@
     }
 
-    /** Comparator, comparing by type and objects display names */
-    static private class OsmPrimitiveComparator implements Comparator<OsmPrimitive> {
-        final private HashMap<OsmPrimitive, String> cache= new HashMap<OsmPrimitive, String>();
-        final private DefaultNameFormatter df  = DefaultNameFormatter.getInstance();
-
-        private String cachedName(OsmPrimitive p) {
-            String name = cache.get(p);
-            if (name == null) {
-                name = p.getDisplayName(df);
-                cache.put(p, name);
-            }
-            return name;
-        }
-
-        private int compareName(OsmPrimitive a, OsmPrimitive b) {
-            String an = cachedName(a);
-            String bn = cachedName(b);
-            // make sure display names starting with digits are the end of the
-            // list
-            if (Character.isDigit(an.charAt(0)) && Character.isDigit(bn.charAt(0)))
-                return an.compareTo(bn);
-            else if (Character.isDigit(an.charAt(0)) && !Character.isDigit(bn.charAt(0)))
-                return 1;
-            else if (!Character.isDigit(an.charAt(0)) && Character.isDigit(bn.charAt(0)))
-                return -1;
-            return an.compareTo(bn);
+    /** Quicker comparator, comparing just by type and ID's */
+    static private class OsmPrimitiveQuickComparator implements Comparator<OsmPrimitive> {
+
+        private int compareId(OsmPrimitive a, OsmPrimitive b) {
+            long id_a=a.getUniqueId();
+            long id_b=b.getUniqueId();
+            if (id_a<id_b) return -1;
+            if (id_a>id_b) return 1;
+            return 0;
         }
 
@@ -956,32 +940,4 @@
         public int compare(OsmPrimitive a, OsmPrimitive b) {
             if (a.getType().equals(b.getType()))
-                return compareName(a, b);
-            return compareType(a, b);
-        }
-    }
-
-    /** Quicker comparator, comparing just by type and ID's */
-    static private class OsmPrimitiveQuickComparator implements Comparator<OsmPrimitive> {
-
-        private int compareId(OsmPrimitive a, OsmPrimitive b) {
-            long id_a=a.getUniqueId();
-            long id_b=b.getUniqueId();
-            if (id_a<id_b) return -1;
-            if (id_a>id_b) return 1;
-            return 0;
-        }
-
-        private int compareType(OsmPrimitive a, OsmPrimitive b) {
-            // show ways before relations, then nodes
-            if (a.getType().equals(OsmPrimitiveType.WAY)) return -1;
-            if (a.getType().equals(OsmPrimitiveType.NODE)) return 1;
-            // a is a relation
-            if (b.getType().equals(OsmPrimitiveType.WAY)) return 1;
-            // b is a node
-            return -1;
-        }
-
-        public int compare(OsmPrimitive a, OsmPrimitive b) {
-            if (a.getType().equals(b.getType()))
                 return compareId(a, b);
             return compareType(a, b);
