Java 在排序方法中忽略符号

Java 在排序方法中忽略符号,java,arrays,sorting,symbols,Java,Arrays,Sorting,Symbols,所以我尝试对一个string arrayglobal变量进行排序,虽然我可以忽略大小写敏感度,但我需要帮助忽略第一个字母之前的符号,例如。 ~abc等 public void sort() { int n = myArray.length; for (int i=0; i<n-1; i++){ for(int j=0; j<n-i-1; j++){ if((myArray[j+1]).compareToIgnoreCase

所以我尝试对一个string arrayglobal变量进行排序,虽然我可以忽略大小写敏感度,但我需要帮助忽略第一个字母之前的符号,例如。 ~abc等

public void sort()
{   
    int n = myArray.length;
    for (int i=0; i<n-1; i++){
        for(int j=0; j<n-i-1; j++){
            if((myArray[j+1]).compareToIgnoreCase(myArray[j])<0){
                String temp = myArray[j];
                myArray[j] = myArray[j+1];
                myArray[j+1] = temp;
                //toLower
            }
        }
    }
}

您可以删除所有特殊字符并进行排序

// Drop all special characters
List<String> collect = Arrays.asList(myArray).stream().map(e -> e.replaceAll("[YourSpeciallCharacterss]", "")).collect(Collectors.toList());
//sort the list
collect.sort(String::compareTo);

您可以通过替换所有匹配的字符来删除控制字符:

\p{Cntrl}-控制字符:[\x00-\x1F\x7F]

下面是一个如何做到这一点的示例:

public static void main(String[] args) throws IOException {

    List<String> list = Arrays.asList("\u0001" + "B", "\u0002" + "AAA", "\u0003" + "AB");
    System.out.println("With control characters: " + list.stream().sorted().collect(Collectors.toList()));
    Pattern removeControl = Pattern.compile("\\p{Cntrl}");
    List<String> sorted = list.stream().map(s -> removeControl.matcher(s)
        .replaceAll("")).sorted().collect(Collectors.toList());
    System.out.println("No control characters: " + sorted);
}

好的,我想出了这个代码。它可以编译,但在运行时陷入了无休止的循环;然而,当我不忽略符号时,排序和写入确实需要18秒,可能是因为有30000个单词。。否则,它就会陷入一个无休止的循环

IO类{

String[] myArray = new String[30000];

public void read()
{

    try {
        Scanner myLocal = new Scanner( new File("dictionary.txt"));  
        while (myLocal.hasNextLine()){

                for (int i=0; i<myArray.length; i++){
                String a = myLocal.nextLine();
                myArray[i] = a;

            }
        }

    }
    catch(IOException e){
        System.out.println(e);
    }
}

public void sort()
{   
    int n = myArray.length;
    String myIgnore = "[^a-zA-Z]+";  // alpha only
    String word1 = "";
    String word2 = "";

    for (int i=0; i<n; i++){
        for(int j=1; j<n-i; j++){ 
        word1 = myArray[j-1].replaceAll(myIgnore,"");
        word2 = myArray[j].replaceAll(myIgnore,"");
        if (word1.compareTo(word2)>0){





    String temp = word1;
    word1=word2;
    word2=temp;



            }

        }


    } 



}

public void write()
{
    try{
        PrintStream writer = new PrintStream(new File("myIgnoreNew.txt"));
        for (int i=0; i<myArray.length; i++){
            writer.println(myArray[i] + "\n");
            }
            writer.close();
    }
    catch(IOException e){
        System.out.println(e);
    }


}

}

您可以编写一个简单的方法,通过仅提取字母来转换字符串,也可以使用regexi。如果您从英语字母表中排序字母,则可以使用非常简单的正则表达式。看看
String[] myArray = new String[30000];

public void read()
{

    try {
        Scanner myLocal = new Scanner( new File("dictionary.txt"));  
        while (myLocal.hasNextLine()){

                for (int i=0; i<myArray.length; i++){
                String a = myLocal.nextLine();
                myArray[i] = a;

            }
        }

    }
    catch(IOException e){
        System.out.println(e);
    }
}

public void sort()
{   
    int n = myArray.length;
    String myIgnore = "[^a-zA-Z]+";  // alpha only
    String word1 = "";
    String word2 = "";

    for (int i=0; i<n; i++){
        for(int j=1; j<n-i; j++){ 
        word1 = myArray[j-1].replaceAll(myIgnore,"");
        word2 = myArray[j].replaceAll(myIgnore,"");
        if (word1.compareTo(word2)>0){





    String temp = word1;
    word1=word2;
    word2=temp;



            }

        }


    } 



}

public void write()
{
    try{
        PrintStream writer = new PrintStream(new File("myIgnoreNew.txt"));
        for (int i=0; i<myArray.length; i++){
            writer.println(myArray[i] + "\n");
            }
            writer.close();
    }
    catch(IOException e){
        System.out.println(e);
    }


}