首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否先按字母顺序小写,再按大写排序?

是否先按字母顺序小写,再按大写排序?
EN

Stack Overflow用户
提问于 2018-07-06 11:44:06
回答 2查看 3.9K关注 0票数 4

我从古腾堡项目的“威廉·莎士比亚的威廉·莎士比亚全集”开始,这是一个可以从http://www.gutenberg.org/ebooks/100获得的UTF8文本文件。在PowerShell中,我运行

代码语言:javascript
复制
Get-Content -Tail 50 $filename | Sort-Object -CaseSensitive

我相信,它将文件的最后50行(即,由换行符分隔的字符串)输送到Sort-Object,它被配置为按字母顺序排序,字符串以小写字母开头,字符串以大写字母开头。

为什么下图中的输出(尤其是P中的)没有根据-CaseSensitive开关进行排序?什么是解决方案?

EN

回答 2

Stack Overflow用户

发布于 2018-07-07 02:05:40

获得所需结果的一种方法是获取每个字符串的第一个字符,并将其转换为Int,这将为您提供该字符的ASCII码,然后您可以将其按所需的顺序进行数字排序。

代码语言:javascript
复制
Get-Content -Tail 50 $filename | Sort-Object -Property @{E={[int]$_[0]};Ascending=$true} 

我们可以使用sort-object-property参数创建一个表达式,我们使用[int]强制转换为int,然后使用$_获取第一个字符以获取管道中的当前字符串/行,然后使用[0]获取该字符串中的第一个字符并将其按升序排序。

这将提供以下输出。

您可能希望修剪输出中的空格,但是,这将留给您来决定。

代码语言:javascript
复制
    DONATIONS or determine the status of compliance for any particular state
    Foundation, how to help produce our new eBooks, and how to subscribe to
    Gutenberg-tm eBooks with only a loose network of volunteer support.
    International donations are gratefully accepted, but we cannot make any
    Most people start at our Web site which has the main PG search facility:
    Project Gutenberg-tm eBooks are often created from several printed
    Please check the Project Gutenberg Web pages for current donation
    Professor Michael S. Hart was the originator of the Project Gutenberg-tm
    Section 5. General Information About Project Gutenberg-tm electronic
    This Web site includes information about Project Gutenberg-tm, including
    While we cannot and do not solicit contributions from states where we
    against accepting unsolicited donations from donors in such states who
    approach us with offers to donate.
    concept of a library of electronic works that could be freely shared
    considerable effort, much paperwork and many fees to meet and keep up
    editions, all of which are confirmed as not protected by copyright in
    have not met the solicitation requirements, we know of no prohibition
    how to make donations to the Project Gutenberg Literary Archive
    including checks, online payments and credit card donations. To donate,
    methods and addresses. Donations are accepted in a number of other ways
    necessarily keep eBooks in compliance with any particular paper edition.
    our email newsletter to hear about new eBooks.
    please visit: www.gutenberg.org/donate
    statements concerning tax treatment of donations received from outside
    the United States. U.S. laws alone swamp our small staff.
    the U.S. unless a copyright notice is included. Thus, we do not
    visit www.gutenberg.org/donate
    with anyone. For forty years, he produced and distributed Project
    www.gutenberg.org
    we have not received written confirmation of compliance. To SEND
    with these requirements. We do not solicit donations in locations where
    works.

更新

首先对小写字母进行排序,然后裁剪空行。从本质上讲,我只是将ascii数乘以一个任意值,这样在数值上它就会高于小写对应的数字。

在示例文本中,没有以特殊字符或标点符号开头的行,可能需要对其进行修改以正确处理这些场景。

代码语言:javascript
复制
Get-Content -Tail 50 $filename | ? { -not [string]::IsNullOrEmpty($_) } | Sort-Object -Property {
    if($_[0] -cmatch "[A-Z]")
    {
        5*[int]$_[0]
    }
    else
    {
        [int]$_[0]
    } 
}

这将输出:

代码语言:javascript
复制
against accepting unsolicited donations from donors in such states who
approach us with offers to donate.
considerable effort, much paperwork and many fees to meet and keep up
concept of a library of electronic works that could be freely shared
editions, all of which are confirmed as not protected by copyright in
how to make donations to the Project Gutenberg Literary Archive
have not met the solicitation requirements, we know of no prohibition
including checks, online payments and credit card donations. To donate,
methods and addresses. Donations are accepted in a number of other ways
necessarily keep eBooks in compliance with any particular paper edition.
our email newsletter to hear about new eBooks.
please visit: www.gutenberg.org/donate
statements concerning tax treatment of donations received from outside
the U.S. unless a copyright notice is included. Thus, we do not
the United States. U.S. laws alone swamp our small staff.
visit www.gutenberg.org/donate
with these requirements. We do not solicit donations in locations where
works.
www.gutenberg.org
with anyone. For forty years, he produced and distributed Project
we have not received written confirmation of compliance. To SEND
DONATIONS or determine the status of compliance for any particular state
Foundation, how to help produce our new eBooks, and how to subscribe to
Gutenberg-tm eBooks with only a loose network of volunteer support.
International donations are gratefully accepted, but we cannot make any
Most people start at our Web site which has the main PG search facility:
Please check the Project Gutenberg Web pages for current donation
Professor Michael S. Hart was the originator of the Project Gutenberg-tm
Project Gutenberg-tm eBooks are often created from several printed
Section 5. General Information About Project Gutenberg-tm electronic
This Web site includes information about Project Gutenberg-tm, including
While we cannot and do not solicit contributions from states where we
票数 3
EN

Stack Overflow用户

发布于 2018-07-15 23:10:06

比较雅各布和mklement0的回答,雅各布的解决方案的优点是视觉上简单,直观,使用流水线,并且可以扩展到按第一个单词的第二个字符或第二个单词的第一个字符排序,等等。mklement0的解决方案的优点是速度更快,让我知道如何先小写再大写排序。

下面我想分享我的雅各布解决方案的扩展,该解决方案按第二个单词的第一个字符排序。对于莎士比亚全集不是特别有用,但对于逗号分隔表非常有用。

代码语言:javascript
复制
Function Replace-Nulls($line) {

 $dump_var = @(
      if ( !($line) ) {
           $line = [char]0 + " " + [char]0 + " [THIS WAS A LINE OF NULL WHITESPACE]"
      } # End if
      if ( !(($line.split())[1]) ) {
           $line += " " + [char]8 + " [THIS WAS A LINE WITH ONE WORD AND THE REST NULL WHITESPACE]"
      } # End if
 ) # End definition of dump_var

 return $line

} # End Replace-Nulls

echo "."
$cleaned_output = Get-Content -Tail 20 $filename | ForEach-Object{ Replace-Nulls($_) }
$cleaned_output | Sort-Object -Property {[int]((($_).split())[1])[0]}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51202710

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档