【问题标题】:Case-insensitive text list sorting不区分大小写的文本列表排序
【发布时间】:2020-06-09 03:40:18
【问题描述】:

有一个“спсВыбора”列表:

("ФайлыКаталоги" "Клиент Проверка Существования Каталога" "Клиент Проверка Существование Файла" "СтандартныеСтруктурыМодуля" "стндОбрОтв" "элКлючаОтветУспехОбрОтв" "элКлючаОтветОшибкаОбрОтв" "элКлючаОтветПроцедура" "элКлючаОтветМодуль" "стндОтчОтв")

要排序,请使用命令:

(setq спсВыбора (sort спсВыбора (lambda (a b) (string> a b))))

因此,“спсВыбора”列表:

("элКлючаОтветУспехОбрОтв" "элКлючаОтветПроцедура" "элКлючаОтветОшибкаОбрОтв" "элКлючаОтветМодуль" "стндОтчОтв" "стндОбрОтв" "ФайлыКаталоги" "СтандартныеСтруктурыМодуля" "Клиент Проверка Существования Каталога" "Клиент Проверка Существование Файла")

排序考虑了小写和大写字母的单独顺序。告诉我如何通过删除案例顺序来对列表进行排序。示例:

"caB" => "aBc"

【问题讨论】:

    标签: elisp


    【解决方案1】:

    使用string-collate-lessp 作为谓词:

    string-collate-lessp is a built-in function in ‘src/fns.c’.
    
    (string-collate-lessp S1 S2 &optional LOCALE IGNORE-CASE)
    
    Return t if first arg string is less than second in collation order.
    Symbols are also allowed; their print names are used instead.
    
    This function obeys the conventions for collation order in your
    locale settings.  For example, punctuation and whitespace characters
    might be considered less significant for sorting:
    
    (sort '("11" "12" "1 1" "1 2" "1.1" "1.2") 'string-collate-lessp)
      => ("11" "1 1" "1.1" "12" "1 2" "1.2")
    
    The optional argument LOCALE, a string, overrides the setting of your
    current locale identifier for collation.  The value is system
    dependent; a LOCALE "en_US.UTF-8" is applicable on POSIX systems,
    while it would be, e.g., "enu_USA.1252" on MS-Windows systems.
    
    If IGNORE-CASE is non-nil, characters are converted to lower-case
    before comparing them.
    
    To emulate Unicode-compliant collation on MS-Windows systems,
    bind ‘w32-collate-ignore-punctuation’ to a non-nil value, since
    the codeset part of the locale cannot be "UTF-8" on MS-Windows.
    
    If your system does not support a locale environment, this function
    behaves like ‘string-lessp’.
    

    【讨论】:

      【解决方案2】:

      如您所见,string< 区分大小写。在您的情况下,我建议获得不区分大小写的排序是对该比较器的大写/小写操作数,以便它实际上不区分大小写:

      (setq спсВыбора (sort спсВыбора (lambda (a b) (string> (downcase a) (downcase b)))))
      

      请注意,根据您的示例,这是反向字母。

      【讨论】:

      • 谢谢 ebpa。起初,我正是在寻找那个。然后想,如果在排序过程中单词不仅包含字母字符,那么这种解决方案很可能不再适合包含
      猜你喜欢
      • 2019-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-17
      • 2020-01-26
      • 2019-01-31
      相关资源
      最近更新 更多