Created
December 28, 2016 06:11
-
-
Save jz5/2467e382dd8fd6ddddca5f89644e1028 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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