Mazlum Ozdogan
Mazlum Ozdogan

Reputation: 218

java sort string array according to kurdish characters

Is there any short way to sort a string array by Kurdish characters? I've looked at some source on internet but I couldn't find any solution. There is a way to sort. Writing a code alike a novel but it is a very long work.

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

Upvotes: 5

Views: 319

Answers (3)

Mazlum Ozdogan
Mazlum Ozdogan

Reputation: 218

I've solved my problem: content of my file was like this:

*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
...

My solution: thîs letters is proper for toLowerCase function:

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

just I was problem. because lowerCase(I) for turkish is ı; but for kurdish it is i.

code:

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

edit:
in AndroidManifest.xml

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

Upvotes: 0

bad
bad

Reputation: 92

You can build your own comparison so that, no matter what characters you are dealing with, it is going to sort the way you want. As you can see from the following code, I have set the comparison value by counting from a-z so that a=0, b=1...etc Then, I used the bubble sort strategy, which is basically switching the smallest elements continuously to the left and shifting others to the right.

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

Upvotes: -1

ivan.sim
ivan.sim

Reputation: 9288

The Collator class should come in-handy here. To quote from the doc,

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

So try something like this:

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

Note that we are able to call java.util.Collections.sort directly as above, because Collator implements the Comparator interface.

If for whatever reasons Locale.UNICODE_LOCALE_EXTENSION doesn't work, here's the full list of supported locales. And you can create your own locale using the Locale constructor.

Upvotes: 6

Related Questions