java根据库尔德字符排序字符串数组

时间:2015-06-04 04:42:41

标签: java arrays sorting

有没有用库尔德字符对字符串数组进行排序的简短方法?我在互联网上看了一些来源,但我找不到任何解决方案。有一种方法可以排序。编写类似于小说的代码,但这是一项非常漫长的工作。

  

库尔德字符:a,b,c,ç,d,e,ê,f,g,h,i,î,j,k,l,m,n,o,p,q,r,s, S,T,U,U,v,W,X,Y,Z

3 个答案:

答案 0 :(得分:6)

Collator课程应该在这里派上用场。引用文档,

  

Collat​​or类执行区分区域敏感的String比较。您可以使用此类为自然语言文本构建搜索和排序例程。

所以尝试这样的事情:

Collator unicodeCollator = Collator.getInstance(Locale.UNICODE_LOCALE_EXTENSION);
Collections.sort(yourListOfCharacters, unicodeCollator);

请注意,我们可以直接调用java.util.Collections.sort,因为Collat​​or实现了Comparator接口。

如果由于Locale.UNICODE_LOCALE_EXTENSION无效的原因,这里是full list of supported locales。您可以使用Locale constructor创建自己的区域设置。

答案 1 :(得分:0)

我已经解决了我的问题:我的文件内容是这样的:

  

*NîzamettînAriç - KardeşTürküler - RojekTê
  Bêxembêşerwelatazadrojektê   Rojekroonahîrojekbişahîrojektê   

Roj Roja我... ....   *KomaÇiya - Tolhildan ^DaketineMeydanê
  Daketinemeydanêgerilayêndînemêr
  Jibonatorhildanêwekbazûpilingûşêr
...

我的解决方案:这些字母适用于toLowerCase功能:

  

ABCÇDEÊFGĞHİÎJKLMNOÖPQRSŞTÛUÜVWXYZ

是个问题。因为土耳其语的lowerCase(I)是ı;但对库尔德来说,它是

代码:

in onCreate():
...
alfabetBike();
...
public static void alfabetBike() {
    for (int i = 0; i < tips.length(); i++) {
        String[] derbasi_arr = sernavs[i];
        String[] derbasi_got = gotins[i];
        for (int j = 0; j < hejmar[i] - 1; j++) {
            int indeks = j;
            String yaMezin = derbasi_arr[j];
            for (int k = j + 1; k < hejmar[i]; k++) {
                if (compareTwoString(yaMezin.substring(1), derbasi_arr[k].substring(1)) > 1) {
                    yaMezin = derbasi_arr[k];
                    indeks = k;
                }
            }
            if (indeks != j) {
                derbasi_arr[indeks] = derbasi_arr[j];
                String derbasi = derbasi_got[indeks];
                derbasi_got[indeks] = derbasi_got[j];
                derbasi_arr[j] = yaMezin;
                derbasi_got[j] = derbasi;
            }
        }
        gotins[i] = derbasi_got;
        sernavs[i] = derbasi_arr;
    }
}

private static void printFile(){
    alfabetBike();
    File root = android.os.Environment.getExternalStorageDirectory();
    File dir = new File (root.getAbsolutePath() + "/alfabetfolder");
    dir.mkdirs();
    File file = new File(dir, "alfabet_title.txt");
    File file2 = new File(dir, "alfabet.txt");

    try {
        FileOutputStream f = new FileOutputStream(file,false);
        PrintWriter pw = new PrintWriter(f);
        FileOutputStream f2 = new FileOutputStream(file2,false);
        PrintWriter pw2 = new PrintWriter(f2);

        for (int i = 0; i < tips.length(); i++) {
            for (int j = 0; j < hejmar[i]; j++) {
                Log.d("ssdddddd", "add" + hejmar[i] + "-" + j + "  " + sernavs[i][j].trim());
                pw.println(sernavs[i][j]);
                pw.flush();
                pw2.println(sernavs[i][j]  + "\n" + gotins[i][j].trim());
                pw2.flush();
            }
        }
        pw.close();
        f.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.i("erroooor", "******* File not found. Did you" +
                " add a WRITE_EXTERNAL_STORAGE permission to the   manifest?");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static int compareTwoString(String yek, String du) {
    String d1 = yek, d2 = du;
    d1 = strLower(d1, d1.charAt(0));
    d2 = strLower(d2, d2.charAt(0));
    int length, yaDirej;

    if (yek.length() > du.length()) {
        yaDirej = 1;
        length = yek.length();
    } else if (yek.length() < du.length()) {
        yaDirej = 2;
        length = du.length();
    } else {
        yaDirej = 0;
        length = yek.length();
    }

    for (int i = 0; i < length; i++) {
        int id1 = -1, id2 = -1;
        if (i == d1.length() || i == du.length()) {
            return yaDirej;
        }
        for (int j = 0; j < tips.length(); j++) {
            if (d1.charAt(i) == tips.charAt(j)) id1 = j;
            if (d2.charAt(i) == tips.charAt(j)) id2 = j;
        }
        if (id1 > id2)
            return 2;
        else if (id2 > id1)
            return 1;
        else
            continue;
    }
    return 0;
}

public static String strLower(String str, char ziman){
    final StringBuilder mutable = new StringBuilder(str);
    final StringBuilder yedek = new StringBuilder(str.toLowerCase());
    for (int i = 0; i < str.length(); i++) {
        if (ziman == '?' && mutable.charAt(i) == 'I')
            mutable.setCharAt(i, 'i');
        else if (ziman == '*' && mutable.charAt(i) == 'I')
            mutable.setCharAt(i, 'ı');
        else mutable.setCharAt(i,yedek.charAt(i));
    }
    return mutable.toString();
}

编辑:
在AndroidManifest.xml中

<manifest...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
....
</manifest...>

答案 2 :(得分:-1)

您可以建立自己的比较,这样无论您处理什么字符,它都会按照您想要的方式排序。从下面的代码中可以看出,我通过从a-z计数来设置比较值,这样a = 0,b = 1 ......等等,然后,我使用了冒泡排序策略,它基本上是切换最小的元素连续向左,并将其他元素向右移动。

public class Sort {

    public static String compare(String compare1, String compare2) {

        for (int i = 0; i < compare1.length(); i++) {
            if (letterValue(compare1, i) < letterValue(compare2, i)) {
                return compare1;
            } else if (letterValue(compare1, i) > letterValue(compare2, i)) {
                return compare2;
            } else if (letterValue(compare1, i) == -1 || letterValue(compare2, i) == -1) {
                System.out.print("Some letters are not within the alphabet!");
            }
        }
        return compare1;
    }

    public static boolean smaller(String compare1, String compare2) {

        if (compare(compare1, compare2).equalsIgnoreCase(compare1)) {
            return true;
        } else {
            return false;
        }
    }

    public static int letterValue(String input, int letterPosition) {

        String order = "abcçdeêfghiîjklmnopqrsştûuvwxyz";
        int value = -1;

        for (int i = 0; i < order.length(); i++) {
            if (input.toLowerCase().charAt(letterPosition) == order.charAt(i)) {
                value = i;
            }
        }
        return value;
    }

    public static void main(String[] args) {
        String[] input = {"BARÊZ", "ÇÊneR", "ASTÛ", "badîn", "BADÎN"};
        String swap;
        int i, d;

        for (i = 0; i < (input.length - 1); i++) {
            for (d = 0; d < input.length - i - 1; d++) {
                if (!smaller(input[d], input[d + 1])) {
                    swap = input[d];
                    input[d] = input[d + 1];
                    input[d + 1] = swap;
                }
            }
        }

        System.out.println("Sorted list: ");

        for (i = 0; i < input.length; i++) {
            System.out.print(input[i] + " ");
        }
    }
}

输出

  

排序列表:

     

ASTÛbadînBADÎNBARÊZÇÊneR