什么是对Text值进行排序的最有效方法?

时间:2014-03-20 01:26:51

标签: sorting haskell

如果我想要排序Data.Text值,我应该将其解压缩到String并使用sort或其他功能吗?当cons和append都是O(n)时,似乎很难为Text值编写快速排序函数。

1 个答案:

答案 0 :(得分:6)

取决于“排序”Text的含义。 Text类型的大部分价值都与其正确处理奇怪的人类语言不一致性有关。考虑这些变体时排序的最佳方法可能是使用text-icu

import           Data.Text.ICU
import qualified Data.Text     as T

-- | Uses the Unicode Collation Algorithm. Others can be chosen by picking 
-- something other than `uca` as your Collator.
sortText :: [T.Text] -> [T.Text]
sortText = sortBy (sortKey uca)

如果你按照你在问题中建议的方式解压缩字符串然后按字典字符顺序比较字符串 - 你可能会慢一些(StringText更笨重)但是如果你有Unicode Text值,你肯定会开辟奇怪的排序和奇怪的重新打包的可能性。

相关问题