Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Collections.Generic;
using System.Globalization;
public class IrohaComparer : IComparer<string>
{
private string iroha = "ィィぃイイいロロろハハはバばパぱニニにホホほボぼポぽヘヘへベべペぺトトとドどチチちヂぢリリりヌヌぬルルるヲヲをヺヮゎワワわヷヵゕカカかガがョョょヨヨよタタたダだレレれソソそゾぞッッっツツつヅづネネねナナなララらムムむゥゥぅウウうヴゔヰゐヸノノのォォぉオオおククくグぐャャゃヤヤやママまヶゖケケけゲげフフふブぶプぷココこゴごェェぇエエえテテてデでァァぁアアあササさザざキキきギぎュュゅユユゆメメめミミみシシしジじヱゑヹヒヒひビびピぴモモもセセせゼぜススすズずンンん";
public int Compare(string x, string y)
{
// null の方が小さい
if (x == null && y == null) return 0;
if (x == null) return -1;
if (y == null) return 1;
// 1文字ずつ比較
var xsi = new StringInfo(x);
var ysi = new StringInfo(y);
var xlen = xsi.LengthInTextElements;
var ylen = ysi.LengthInTextElements;
var xidx = StringInfo.ParseCombiningCharacters(x);
var yidx = StringInfo.ParseCombiningCharacters(y);
for (int i = 0; i < Math.Min(xlen, ylen); i++)
{
var xc = StringInfo.GetNextTextElement(x, xidx[i]);
var yc = StringInfo.GetNextTextElement(y, yidx[i]);
if (xc == yc) continue;
var xi = iroha.IndexOf(xc);
if (xi < 0) return xc.CompareTo(yc);
var yi = iroha.IndexOf(yc);
if (yi < 0) return xc.CompareTo(yc);
if (xi < yi) return -1;
if (xi > yi) return 1;
}
// 文字数の短い方が小さい
if (xlen < ylen) return -1;
if (xlen > ylen) return 1;
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment