Java 数组的子串排序

Java 数组的子串排序,java,arrays,sorting,Java,Arrays,Sorting,我需要按子字符串对数组进行排序,并输入缺少的部分 当前版本: “K1\SK1” “K1\SK2” “K1\SK1\SSK1” “K1\SK1\SSK2” “K2” “K2\SK1\SSK1” “K2\SK1\SSK2” 预期版本: “K1” “K1\SK1” “K1\SK1\SSK1” “K1\SK1\SSK2” “K1\SK2” “K2” “K2\SK1” “K2\SK1\SSK1” “K2\SK1\SSK2” 我已经尝试过通过(“/”)将字符串拆分为数组,然后分别对数组的每个部分进行排序。但

我需要按子字符串对数组进行排序,并输入缺少的部分

当前版本:
“K1\SK1”

“K1\SK2”

“K1\SK1\SSK1”

“K1\SK1\SSK2”

“K2”

“K2\SK1\SSK1”

“K2\SK1\SSK2”

预期版本:

“K1”

“K1\SK1”

“K1\SK1\SSK1”

“K1\SK1\SSK2”

“K1\SK2”

“K2”

“K2\SK1”

“K2\SK1\SSK1”

“K2\SK1\SSK2”

我已经尝试过通过(“/”)将字符串拆分为数组,然后分别对数组的每个部分进行排序。但最终它太麻烦了,算法也太难了

也许有人知道解决这个问题的更简单的方法


谢谢大家!

您可以创建一个排序的
集,即
树集
,用于存储结果

然后迭代输入字符串,并将每个字符串添加到结果
。您还可以扫描所有
\
字符,对于找到的每个
\
字符,从字符串开头抓取子字符串,直到但不包括
\
,并将其添加到结果
集合中


集合
现在包含您要查找的结果。如果需要,您可以将其提取为数组。

这里是Python代码,而不是JAVA,但我认为这段代码很容易适应排序集的使用,正如我所写的那样,我尽可能地不言自明:

def lstAllPositionsOfSeparatorCharInString(strSepChar, strToQuery): 
    """Return a list with all the positions of a character strSepChar in a string strToQuery."""
    lstPositions = []
    intPos = strToQuery.find(strSepChar)
    while intPos != -1: # strSepChar was found, proceed
        lstPositions.append(intPos)
        intPos = strToQuery.find(strSepChar, intPos+1)
    #:while
    return lstPositions
#:def

lstStrings=[r"K1\SK1", r"K1\SK2", r"K1\SK1\SSK1", r"K1\SK1\SSK2", r"K2", r"K2\SK1\SSK1", r"K2\SK1\SSK2"]
print(lstStrings)
# Append missing items if any: 
strSepChar = r"\ "[0] # strSepChar will contain a single backslash
for item in lstStrings: 
    for posNo in lstAllPositionsOfSeparatorCharInString(strSepChar, item):
        if item[0:posNo] not in lstStrings: 
            lstStrings.append(item[0:posNo])
#:for :for :if
print( lstStrings )
# sort the list (in place):
lstStrings.sort()
print( lstStrings )
代码首先输出原始列表,然后是扩展列表,最后是排序列表(集合):


您可以尝试以下代码,并且还需要一些导入

     public class Sort {  
/**
 * Sort.
 * @param list original list
 */
public void sortByDirectOrder(List<String> list) {
    checkContainsAll(list);
    list.sort(new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            int result;
            int length1 = o1.length();
            int length2 = o2.length();
            int length = length1 < length2 ? length1 : length2;
            int subResult = o1.substring(0, length).compareTo(o2.substring(0, length));
            if (subResult == 0) {
                 result = length1 < length2 ? -1 : length2 < length1 ? 1 : 0;
            } else {
                 result = subResult;
            }
            return result;
        }
        });      
    }

/**
 * Method for check and complement list of codes.
 * @param list original list
 */
private void checkContainsAll(List<String> list) {
    List<String> additionalList = new ArrayList<>();
    String substring;
    for (String code : list) {
        if (code.contains("\\")) {
            for (int index = 0; index != code.length(); index++) {
                index = code.indexOf("\\", index);
                if (index == -1) {
                    break;
                }
                substring = code.substring(0, index);
                if (!list.contains(substring) && !additionalList.contains(substring)) {
                    additionalList.add(substring);
                }
            }
        }
    }
    if (additionalList.size() > 0) {
        list.addAll(additionalList);
    }
}
}
公共类排序{
/**
*排序。
*@param list原始列表
*/
公共无效排序编辑器(列表){
检查所有(列表);
list.sort(新的比较器(){
@凌驾
公共整数比较(字符串o1、字符串o2){
int结果;
int length1=o1.length();
int length2=o2.length();
整数长度=长度1<长度2?长度1:长度2;
int subResult=o1.子字符串(0,长度).compareTo(o2.子字符串(0,长度));
如果(子结果==0){
结果=长度10){
list.addAll(附加列表);
}
}
}

显示您所做的尝试我不明白为什么
字符串上的默认排序在这里不起作用。你能告诉我更多的输入信息吗?你说的“输入缺少的部分”是什么意思?1)当文本是
\
而不是
/
时,为什么要按(“/”)拆分?2) 添加额外值意味着您没有排序。排序是按不同顺序重新排列的行为。排序不会向列表中添加新值。@doctorlove OP意味着将从以
K1\…
开头的路径派生的
“K1”
添加到列表中。将输入视为限定的文件名,OP想知道要创建哪些目录。
     public class Sort {  
/**
 * Sort.
 * @param list original list
 */
public void sortByDirectOrder(List<String> list) {
    checkContainsAll(list);
    list.sort(new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            int result;
            int length1 = o1.length();
            int length2 = o2.length();
            int length = length1 < length2 ? length1 : length2;
            int subResult = o1.substring(0, length).compareTo(o2.substring(0, length));
            if (subResult == 0) {
                 result = length1 < length2 ? -1 : length2 < length1 ? 1 : 0;
            } else {
                 result = subResult;
            }
            return result;
        }
        });      
    }

/**
 * Method for check and complement list of codes.
 * @param list original list
 */
private void checkContainsAll(List<String> list) {
    List<String> additionalList = new ArrayList<>();
    String substring;
    for (String code : list) {
        if (code.contains("\\")) {
            for (int index = 0; index != code.length(); index++) {
                index = code.indexOf("\\", index);
                if (index == -1) {
                    break;
                }
                substring = code.substring(0, index);
                if (!list.contains(substring) && !additionalList.contains(substring)) {
                    additionalList.add(substring);
                }
            }
        }
    }
    if (additionalList.size() > 0) {
        list.addAll(additionalList);
    }
}
}