Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 403
Mazlum Özdoğan Avatar asked Jun 04 '15 04:06

Mazlum Özdoğan


People also ask

Can we sort string array in Java?

To sort an array of strings in Java, we can use Arrays. sort() function.

Can you sort an array of characters in Java?

sort(char[] a, int fromIndex, int toIndex) method sorts the specified range of the specified array of chars into ascending numerical order. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive.

How do you sort an array of strings?

There are two ways to sort a string array in Java: Using User-Defined Logic. Using the Arrays. sort() Methodm.

How do you sort an array of strings alphabetically in Java?

Using the toCharArray() method Get the required string. Convert the given string to a character array using the toCharArray() method. Sort the obtained array using the sort() method of the Arrays class. Convert the sorted array to String by passing it to the constructor of the String array.


1 Answers

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.

like image 189
ivan.sim Avatar answered Oct 07 '22 08:10

ivan.sim