LineStringSorter { public static List<ILineString> sort(ILineString line, Collection<ILineString> collection) { final Map<ILineString, Double> paramForLineString = new HashMap<>(); for (ILineString l : collection) { if (line.intersects(l)) { IGeometry intersection = line.intersection(l); if (intersection == null) { continue; } double minParam = Double.POSITIVE_INFINITY; for (IDirectPosition p : intersection.coord()) { double param = line.paramForPoint(p)[0]; minParam = Math.min(minParam, param); } paramForLineString.put(l, minParam); } } Comparator<ILineString> comparator = (o1, o2) -> { double p1 = paramForLineString.get(o1); double p2 = paramForLineString.get(o2); return Double.compare(p1, p2); }; List<ILineString> result = new ArrayList<>(paramForLineString.keySet()); result.sort(comparator); return result; }  static List<ILineString> sort(ILineString line, Collection<ILineString> collection); static List<ILineString> sort(IDirectPosition point, Collection<ILineString> collection);  }
@Test public void testSort() { List<ILineString> result = LineStringSorter.sort(line1, Arrays.asList(line2, line3, line4, line5)); Assert.assertEquals(Arrays.asList(line4, line2, line3), result); ILineString l = new GM_LineString( new DirectPosition(651128.2399543846, 6861330.51367064), new DirectPosition(651128.2399543575, 6861330.513667467), new DirectPosition(651126.8, 6861362.7), new DirectPosition(651124.9, 6861379.5) ); ILineString l1 = new GM_LineString( new DirectPosition(651122.5650124006, 6861360.588720706), new DirectPosition(651131.5104028855, 6861365.078859089) ); ILineString l2 = new GM_LineString( new DirectPosition(651131.6845765973, 6861360.470596566), new DirectPosition(651122.7929930952, 6861360.134530621) ); ILineString l3 = new GM_LineString( new DirectPosition(651123.5411303148, 6861357.7533064), new DirectPosition(651131.6902512497, 6861360.3204574445) ); ILineString l4 = new GM_LineString( new DirectPosition(651131.7755160853, 6861358.064532925), new DirectPosition(651123.5411303148, 6861357.7533064) ); ILineString l5 = new GM_LineString( new DirectPosition(651132.0299365349, 6861351.333113982), new DirectPosition(651124.042100489, 6861351.031206041) ); ILineString l6 = new GM_LineString( new DirectPosition(651124.0589372854, 6861350.533388453), new DirectPosition(651132.049947978, 6861350.803654156) ); ILineString l7 = new GM_LineString( new DirectPosition(651132.1906160566, 6861347.081878759), new DirectPosition(651124.1859040088, 6861346.779332973) ); ILineString l8 = new GM_LineString( new DirectPosition(651124.2198938279, 6861345.774347863), new DirectPosition(651132.2297963421, 6861346.045252511) ); ILineString l9 = new GM_LineString( new DirectPosition(651132.3512955783, 6861342.830643537), new DirectPosition(651124.3297075287, 6861342.527459906) ); ILineString l10 = new GM_LineString( new DirectPosition(651124.3808503704, 6861341.015307275), new DirectPosition(651132.4096447063, 6861341.286850867) ); ILineString l11 = new GM_LineString( new DirectPosition(651132.2142040946, 6861337.837054882), new DirectPosition(651124.4735110486, 6861338.275586839) ); ILineString l12 = new GM_LineString( new DirectPosition(651124.5907630872, 6861335.499412037), new DirectPosition(651132.0997370766, 6861335.816554354) ); ILineString l13 = new GM_LineString( new DirectPosition(651131.9308314561, 6861333.578202283), new DirectPosition(651124.6486970368, 6861334.127710758) ); List<ILineString> list = Arrays.asList(l1, l2, l3, l8, l4, l12, l5, l11, l10, l6, l9, l7, l13); Collections.shuffle(list); result = LineStringSorter.sort(l, list); Assert.assertEquals(Arrays.asList(l13, l12, l11, l10, l9, l8, l7, l6, l5, l4, l3, l2, l1), result); ILineString ll = new GM_LineString( new DirectPosition(651036.3, 6861368.800000001, Double.POSITIVE_INFINITY), new DirectPosition(651039.4, 6861386.800000001, 0), new DirectPosition(651047.7000000001, 6861432.800000001, 0), new DirectPosition(651048.49003641, 6861442.991466861, 0), new DirectPosition(651048.4900364373, 6861442.991470035, 0)); ILineString ll1 = new GM_LineString( new DirectPosition(651044.082420469, 6861436.968906329), new DirectPosition(651054.2437587668, 6861435.603961451)); ILineString ll2 = new GM_LineString( new DirectPosition(651035.4940586238, 6861383.924357968), new DirectPosition(651047.1717791874, 6861381.963138461)); list = Arrays.asList(ll1, ll2); result = LineStringSorter.sort(ll, list); Assert.assertEquals(Arrays.asList(ll2, ll1), result); }