From 0980ad3fe9883f503c2dd764bd1cb7c1adeea9bd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=BCrg=20Lehni?= <juerg@scratchdisk.com>
Date: Wed, 16 Sep 2015 10:44:41 +0200
Subject: [PATCH] Fix remaining issues with curve location sorting.

Relates to #787
---
 src/path/CurveLocation.js | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/path/CurveLocation.js b/src/path/CurveLocation.js
index e8f17c4c..a9fe06f6 100644
--- a/src/path/CurveLocation.js
+++ b/src/path/CurveLocation.js
@@ -403,16 +403,22 @@ var CurveLocation = Base.extend(/** @lends CurveLocation# */{
                 if (path1 === path2) {
                     if (curve1 === curve2) {
                         var diff = l1._parameter - l2._parameter;
+                        // TODO: Compare points instead of parameter like in
+                        // equals? or curve time there too? Why was it changed?
                         if (Math.abs(diff) < /*#=*/Numerical.CURVETIME_EPSILON){
                             var i1 = l1._intersection,
                                 i2 = l2._intersection,
                                 curve21 = i1 && i1._curve,
-                                curve22 = i2 && l2._curve;
+                                curve22 = i2 && i2._curve,
+                                path21 = curve21 && curve21._path,
+                                path22 = curve22 && curve22._path;
                             res = curve21 === curve22 // equal or both null
                                 ? i1 && i2 ? i1._parameter - i2._parameter : 0
                                 : curve21 && curve22
-                                    ? curve21.getIndex() - curve22.getIndex()
-                                    : curve21 ? 1 : -1;
+                                    ? path21 === path22
+                                        ? curve21.getIndex() - curve22.getIndex()
+                                        : curve21 ? 1 : -1
+                                    : path21._id - path22._id;
                         } else {
                             res = diff;
                         }