【问题标题】:java sort string array according to kurdish charactersjava根据库尔德字符对字符串数组进行排序
【发布时间】:2015-06-04 04:42:41
【问题描述】:

有没有什么捷径可以按库尔德字符对字符串数组进行排序?我在互联网上查看了一些资源,但找不到任何解决方案。有一种方法可以排序。编写代码就像小说一样,但这是一项非常漫长的工作。

库尔德字符:a,b,c,ç,d,e,ê,f,g,h,i,î,j,k,l,m,n,o,p,q,r,s, ş,t,û,u,v,w,x,y,z

【问题讨论】:

  • @Mazlum 如果您喜欢我的回答,请考虑将其标记为已接受。谢谢。
  • 我试过了,但它说 java.locale 不能应用于 char。在这个错误之后,我没有做太多的工作。但我会再看一遍

标签: java arrays sorting


【解决方案1】:

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

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

所以试试这样的:

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 创建自己的语言环境。

【讨论】:

  • 整理器 unicodeCollat​​or = Collat​​or.getInstance(Locale.UNICODE_LOCALE_EXTENSION);错误:(145, 63) 错误:不兼容的类型:char 无法转换为 Locale
  • @MazlumÖzdoğan 您可以尝试使用我在回答中提到的链接中定义的其他受支持的语言环境。
【解决方案2】:

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

*Nîzamettîn Ariç - Kardeş Türküler - Rojek Tê
Bê xem bê şer welat azad rojek tê Rojek ronahî rojek bişahî rojek tê

Roj Roja 我你....
*Koma Çiya - Tolhildan ^ Daketine Meydanê
Daketine meydanê gerilayên dînemêr
Ji bona tolhildanê wek baz û piling û şêr
...

我的解决方案:这些字母适合 toLowerCase 函数:

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

只是有问题。因为土耳其语的小写(I)是ı;但对于库尔德人来说,它是 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...>

【讨论】:

    【解决方案3】:

    您可以建立自己的比较,这样,无论您处理什么字符,它都会按照您想要的方式进行排序。从下面的代码可以看出,我已经通过从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în BADÎN BARÊZ ÇÊneR

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多