src/main/java/de/blau/android/util/Util.java
/**
     * Sort a list of RelationMemberDescription in the order they are connected
     * 
     * Note: there is likely a far better algorithm than this, ignores way direction.
     * 
     * @param list List of relation members
     * @return fully or partially sorted List of RelationMembers, if partially sorted the unsorted elements will come
     *         first
     * @param <T> Class that extents RelationMember
     */
Sort a list of RelationMemberDescription in the order they are connected

there is likely a far better algorithm than this, ignores way direction.

@param list List of relation members
@return fully or partially sorted List of RelationMembers, if partially sorted the unsorted elements will come
first
@param  Class that extents RelationMember
Sort a list of RelationMemberDescription in the order they are connected there is likely a far better algorithm than this ignores way direction . @param list List of relation members @return fully or partially sorted List of RelationMembers if partially sorted the unsorted elements will come first @param Class that extents RelationMember
@NonNull
    public static <T extends RelationMember> List<T> sortRelationMembers(@NonNull List<T> list) {
        List<T> result = new ArrayList<>();
        List<T> unconnected = new ArrayList<>(list);
        int nextWay = 0;
        while (true) {
            nextWay = nextWay(nextWay, unconnected);
            if (nextWay >= unconnected.size()) {
                break;
            }
            T currentRmd = unconnected.get(nextWay);
            unconnected.remove(currentRmd);
            result.add(currentRmd);
            int start = result.size() - 1;
            for (int i = nextWay; i < unconnected.size();) {
                T rmd = unconnected.get(i);
                if (!rmd.downloaded() || !Way.NAME.equals(rmd.getType())) {
                    i++;
                    continue;
                }
                Way startWay = (Way) result.get(start).getElement();
                Way endWay = (Way) result.get(result.size() - 1).getElement();
                Way currentWay = (Way) rmd.getElement();
                if (haveCommonNode(endWay, currentWay)) {
                    result.add(rmd);
                    unconnected.remove(rmd);
                } else if (haveCommonNode(startWay, currentWay)) {
                    result.add(start, rmd);
                    unconnected.remove(rmd);
                } else {
                    i++;
                }
            }
        }
        unconnected.addAll(result); 
        return unconnected;
    }
@ NonNull public static < T extends RelationMember > List < T > sortRelationMembers ( @ NonNull List < T > list ) { List < T > result = new ArrayList < > ( ) ; List < T > unconnected = new ArrayList < > ( list ) ; int nextWay = 0 ; while ( true ) { nextWay = nextWay ( nextWay , unconnected ) ; if ( nextWay >= unconnected . size ( ) ) { break ; } T currentRmd = unconnected . get ( nextWay ) ; unconnected . remove ( currentRmd ) ; result . add ( currentRmd ) ; int start = result . size ( ) - 1 ; for ( int i = nextWay ; i < unconnected . size ( ) ; ) { T rmd = unconnected . get ( i ) ; if ( ! rmd . downloaded ( ) || ! Way . NAME . equals ( rmd . getType ( ) ) ) { i ++ ; continue ; } Way startWay = ( Way ) result . get ( start ) . getElement ( ) ; Way endWay = ( Way ) result . get ( result . size ( ) - 1 ) . getElement ( ) ; Way currentWay = ( Way ) rmd . getElement ( ) ; if ( haveCommonNode ( endWay , currentWay ) ) { result . add ( rmd ) ; unconnected . remove ( rmd ) ; } else if ( haveCommonNode ( startWay , currentWay ) ) { result . add ( start , rmd ) ; unconnected . remove ( rmd ) ; } else { i ++ ; } } } unconnected . addAll ( result ) ; return unconnected ; }
Sort a list of RelationMemberDescription in the order they are connected
Sort a list of RelationMemberDescription in the order they are connected
// the following works for all situation including closed ways but will be a bit slow // return with unsorted elements at top