2015-06-04 16 views
5

Có cách nào ngắn để sắp xếp mảng chuỗi theo ký tự tiếng Kurd không? Tôi đã xem xét một số nguồn trên internet nhưng tôi không thể tìm thấy bất kỳ giải pháp. Có một cách để sắp xếp. Viết một mã giống như một cuốn tiểu thuyết nhưng nó là một công việc rất dài.mảng chuỗi sắp xếp java theo ký tự kurdish

kurdish characters: 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

+2

thực hiện so sánh của riêng bạn https://docs.oracle.com/javase/7/docs/api/java /util/Comparator.html –

+0

@Mazlum Nếu bạn thích câu trả lời của tôi, hãy xem xét đánh dấu nó là được chấp nhận. Cảm ơn. –

+0

Tôi đã thử nhưng nó nói java.locale không thể được áp dụng cho char. Sau khi lỗi này tôi đã không làm việc trên nó quá nhiều. Nhưng tôi sẽ xem xét lại –

Trả lời

0

Tôi đã giải quyết vấn đề của tôi: Nội dung của tập tin của tôi là như thế này:

*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 me ye....
*Koma Çiya - Tolhildan^Daketine Meydanê
Daketine meydanê gerilayên dînemêr
Ji bona tolhildanê wek baz û piling û şêr
...

Giải pháp của tôi: Đây là chữ thích hợp cho hàm toLowerCase:

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

chỉ I là vấn đề. bởi vì lowerCase (I) cho Thổ Nhĩ Kỳ là ı; nhưng đối với người Kurd là i.

mã:

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(); 
} 

chỉnh sửa:
trong AndroidManifest.xml

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

Lớp học Collator sẽ có ích ở đây. Trích dẫn từ doc,

The Collator class performs locale-sensitive String comparison. You use this class to build searching and sorting routines for natural language text.

Vì vậy, thử một cái gì đó như thế này:

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

Lưu ý rằng chúng tôi có thể gọi trực tiếp java.util.Collections.sort như trên, vì collator cài đặt giao diện sánh.

Nếu vì bất kỳ lý do gì Locale.UNICODE_LOCALE_EXTENSION không hoạt động, dưới đây là full list of supported locales. Và bạn có thể tạo miền địa phương của riêng mình bằng cách sử dụng Locale constructor.

+0

Collator unicodeCollator = Collator.getInstance (Locale.UNICODE_LOCALE_EXTENSION); Lỗi: (145, 63) lỗi: loại không tương thích: char không thể được chuyển đổi sang Locale –

+0

@ MazlumÖzdoğan Bạn có thể thử với các ngôn ngữ được hỗ trợ khác được xác định trong liên kết tôi đã đề cập trong câu trả lời của mình. –

+0

cảm ơn bạn. Tôi đã giải quyết được vấn đề của mình. xem nhận xét sau .. –

-1

Bạn có thể xây dựng so sánh của riêng mình để, bất kể bạn đang xử lý những ký tự nào, nó sẽ sắp xếp theo cách bạn muốn. Như bạn có thể thấy từ đoạn mã sau, tôi đã đặt giá trị so sánh bằng cách đếm từ a-z sao cho a = 0, b = 1 ... vv Sau đó, tôi đã sử dụng chiến lược sắp xếp bong bóng, về cơ bản là chuyển đổi các phần tử nhỏ nhất liên tục bên trái và dịch chuyển sang bên phải.

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] + " "); 
 
     } 
 
    } 
 
}

Output

Sorted list:

ASTÛ badîn BADÎN BARÊZ ÇÊneR

Các vấn đề liên quan