Java – custom sorting string list (following Chamorro language sorting rules)

I'm trying to sort the string list for the Pacific island language (Chamorro) In this language, Ng is considered a letter after N in the alphabet How do I sort the word lists Nai and Nunu so that they appear before the words beginning with ng?

to update

The complete alphabet is:

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

Except Å, Ñ and their lowercase versions, there is no accent on other letters Words can contain apostrophes (such as o'mak), but they do not affect the sort order

Chamorro has no locale, so I need to implement the sorting algorithm manually

Solution

Thanks to Dirk lachowski, I implemented an effective solution This 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 implemented the rule-based collator above, I just 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);
      }

    });
  }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>