Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails ruby中基于区域设置的排序函数_Ruby On Rails_Ruby_Internationalization - Fatal编程技术网

Ruby on rails ruby中基于区域设置的排序函数

Ruby on rails ruby中基于区域设置的排序函数,ruby-on-rails,ruby,internationalization,Ruby On Rails,Ruby,Internationalization,对于我的应用程序(RubyonRails),我在注册页面上有国家选择框。这些国家被本地化为不同的语言。但是我找不到一种方法来对它们进行分类,基于它的本地化语言。目前我只根据英语把它整理出来。有没有办法根据地区对国家名称进行排序?i、 e国家的顺序应根据其本地化语言的不同而变化(升序)。 谢谢。也许您可以翻译所有内容,并在翻译后对其进行排序。您可以基于给定的字母表,创建一个自定义的字符串比较方法,类似这样的方法(适用于Ruby 1.9): 类字符串 #根据给定的字母表比较两个字符串 def cmp

对于我的应用程序(RubyonRails),我在注册页面上有国家选择框。这些国家被本地化为不同的语言。但是我找不到一种方法来对它们进行分类,基于它的本地化语言。目前我只根据英语把它整理出来。有没有办法根据地区对国家名称进行排序?i、 e国家的顺序应根据其本地化语言的不同而变化(升序)。
谢谢。

也许您可以翻译所有内容,并在翻译后对其进行排序。

您可以基于给定的字母表,创建一个自定义的
字符串比较方法,类似这样的方法(适用于Ruby 1.9):

类字符串
#根据给定的字母表比较两个字符串
def cmp_loc(其他,字母表)
顺序=散列[字母表。每个字符带有索引到]
self.chars.zip(其他.chars)do | c1,c2|
cc=(命令[c1]| |-1)(命令[c2]| |-1)
返回cc,除非cc==0
结束
返回self.size其他.size
结束
结束
类数组
#根据给定的字母表对字符串数组进行排序
def sort_loc(字母表)
self.sort{| s1,s2 | s1.cmp_loc(s2,字母表)}
结束
结束
数组_to_sort=['abc','abd','bcd','bcde','bde']
字母表={
:language_foo=>“abcdef”,
:language_bar=>“fedcba”
}
p array\u to\u sort.sort\u loc(字母表[:language\u foo])
#=>[“abc”、“abd”、“bcd”、“bcde”、“bde”]
p数组到排序。排序位置(字母表[:语言\u栏])
#=>[“bde”、“bcd”、“bcde”、“abd”、“abc”]

然后为您想要支持的每种语言提供字母顺序。

前一段时间,twitter发布了一个库,它可以很好地在Ruby中为多种语言处理它,而且它确实可以工作。它们还提供了一种更高级别的排序方法,以及一种只需比较给定区域设置的两个字符串的低级排序方法,这也是非常好的。这让我摆脱了git://github.com/k3rni/ffi-locale.git 到目前为止,我一直在使用它以区域设置感知的方式对字符串进行排序。

但是,如果lang是中文,如何根据中文对其进行排序?可能重复:
class String
  # compares two strings based on a given alphabet
  def cmp_loc(other, alphabet)
    order = Hash[alphabet.each_char.with_index.to_a]

    self.chars.zip(other.chars) do |c1, c2|
      cc = (order[c1] || -1) <=> (order[c2] || -1)
      return cc unless cc == 0
    end
    return self.size <=> other.size
  end
end

class Array
  # sorts an array of strings based on a given alphabet
  def sort_loc(alphabet)
    self.sort{|s1, s2| s1.cmp_loc(s2, alphabet)}
  end
end

array_to_sort = ['abc', 'abd', 'bcd', 'bcde', 'bde']

ALPHABETS = {
  :language_foo => 'abcdef',
  :language_bar => 'fedcba'
}

p array_to_sort.sort_loc(ALPHABETS[:language_foo])
#=>["abc", "abd", "bcd", "bcde", "bde"]

p array_to_sort.sort_loc(ALPHABETS[:language_bar])
#=>["bde", "bcd", "bcde", "abd", "abc"]