Custom String Sort List (following Chamorro language sorting rules)

I am trying to sort a list of strings for a Pacific island language (Chamorro). In this language, Ng is considered a single letter, and it appears after N in the alphabet. How can I sort the list of words so that Nai and Nunu both precede words starting with Ng ?

Update

Full alphabet:

 A, Γ…, B, Ch, D, E, F, G, H, I, K, L, M, N, Γ‘, Ng, O, P, R, S, T, U, Y 

Apart from Γ… , Γ‘ and their lowercase versions, there are no other accents over other letters. Words can have apostrophes in them (for example, o'mak ), but they do not affect the sort order.

There is no locale for Chamorro, so I need to manually implement the sorting algorithm.

+7
java internationalization collation
source share
1 answer

Thanks to Dirk Lachowski, I implemented a solution that works. Here is what I wrote:

  static final String CHAMORRO_RULES = ("< a,A < Γ₯,Γ… < b,B < ch,Ch < d,D < e,E < f,F < g,G < h,H < i,I < k,K < l,L " + "< m,M < n,N < Γ±,Γ‘ < ng,Ng < o,O < p,P < r,R < s,S < t,T < u,U < y,Y"); static final RuleBasedCollator CHAMORRO_COLLATOR; static { try { CHAMORRO_COLLATOR = new RuleBasedCollator(CHAMORRO_RULES); } catch (ParseException pe) { throw new RuntimeException(pe); } } 

After I applied the rule-based collator above, I simply wrote the following sorting method:

  static void sort(List<String> words) { Collections.sort(words, new Comparator<String>() { @Override public int compare(String lhs, String rhs) { return Constants.CHAMORRO_COLLATOR.compare(lhs, rhs); } }); } 
+4
source share

All Articles