class 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;
  }

}