标签: sorting

如何按降序按特定属性对自定义对象的 NSArray 进行排序?

如何使这段代码按降序排列。这段代码总是按升序给我数组:

NSArray *sortedProductsByStyle = [unsortedProducts sortedArrayUsingComparator: ^(Product *p1, Product *p2) {
        return [p1.productStyle compare:p2.productStyle options:NSNumericSearch];
    }];
Run Code Online (Sandbox Code Playgroud)

我认为使用 NSOrderedDescending 会起作用,但它没有:

NSArray *sortedProductsByStyle = [unsortedProducts sortedArrayUsingComparator: ^(Product *p1, Product *p2) {
        return [p1.productStyle compare:p2.productStyle options:NSNumericSearch | NSOrderedDescending];
    }];
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

sorting compare nsarray ios

1
推荐指数
1
解决办法
588
查看次数

在 Mongodb 中永久排序集合

每当我们执行 db.Collection.find().sort() 时,只有我们的输出被排序,而不是集合本身,

即如果我做 db.collection.find() 然后我看到原始集合,而不是排序的集合。

有没有办法对集合本身进行排序,而不仅仅是对输出进行排序?

将排序结果导出到整个新集合中也可以。

如果我对 _id 字段进行了编号。(例如 _id:1 、 _id_2 、 _id:3 等等)

sorting mongodb

1
推荐指数
1
解决办法
3996
查看次数

Windows cmd 按文件类型对文件进行排序并将它们存储在文件夹中

我想要一个批处理文件,它将按文件类型对文件进行排序并将它们分类到文件夹中。例如,我将在某个文件夹中运行此批处理文件,而 .PDF 文件将保存在“PDF”文件夹中,与其他文件类型相同。可以在命令行中做到这一点吗?谢谢你。

sorting cmd file-type batch-file

1
推荐指数
1
解决办法
4849
查看次数

根据python中单词中的元音数对列表进行排序

如何根据python中单词中的元音数对列表进行排序?

我没有在任何网站上找到答案。这个词应该按照元音数量降序排列。

python sorting list python-2.7

1
推荐指数
1
解决办法
4018
查看次数

Array.Sort 与自定义 IComparer 接口

我编写了以下实现 IComparer 的类:

class CustomComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {
        int intX;
        int intY;

        if (int.TryParse(x, out intX) && int.TryParse(y, out intY)) //both numbers
        {
            intX.CompareTo(intY);
        }
        else if (!int.TryParse(x, out intX) && !int.TryParse(y, out intY)) //both letters
        {
            x.CompareTo(y);
        }
        else if (!int.TryParse(x, out intX) && int.TryParse(y, out intY)) //first is a letter, second a number
        {
            return -1;
        }
        else if (int.TryParse(x, out intX) && !int.TryParse(y, out intY)) //first is a number, second …
Run Code Online (Sandbox Code Playgroud)

c# sorting arraylist icomparer

1
推荐指数
1
解决办法
1201
查看次数

D3.js 按数字排序表,然后按字母顺序(作为决胜局)

我有一个包含两列数据的表格。当我用列 [1] 排序时,我需要根据列 [0] 的字母值进一步排序。

这将按第二列中的浮点值对表格进行排序,但我想进一步按第一列中的字符串值按字母顺序对领带进行排序。

return tbody.selectAll("tr").sort(function(a, b) 
{
    isSorted = true;
    d3.ascending(parseFloat(a[1]), parseFloat(b[1]));
})
Run Code Online (Sandbox Code Playgroud)

javascript sorting d3.js

1
推荐指数
1
解决办法
3960
查看次数

IQueryable 排序不起作用

下面的代码在排序方面有什么问题?排序代码被命中,但排序从未应用于结果。

var results = new List<Location>();

var county = context.boc_County.Where(x => x.Description.Contains(phrase.ToLower())).ToList();
results.AddRange(_mapper.MapCountyFromDb(county));

var town = context.boc_Town.Where(x => x.Description.Contains(phrase.ToLower())).ToList();
results.AddRange(_mapper.MapTownFromDb(town));

if (orderBy == "Identifier")
{
    if (direction == "ASC")
        results = results.OrderBy(x => x.Identifier);
    else
        results = results.OrderByDescending(x => x.Identifier);
}

if (orderBy == "Type")
{
    if (direction == "ASC")
        results = results.OrderBy(x => x.LocationType.ToString());
    else
        results = results.OrderByDescending(x => x.LocationType.ToString());
}

if (orderBy == "Description")
{
    if (direction == "ASC") 
        results = results.OrderBy(x => x.Description);
    else
        results = results.OrderByDescending(x …
Run Code Online (Sandbox Code Playgroud)

linq sorting iqueryable

1
推荐指数
1
解决办法
1987
查看次数

在 Python 中按创建日期对文件列表进行排序时出现奇怪的错误

我一直在关注本教程http://code.activestate.com/recipes/576804-find-the-oldest-or-yougest-of-a-list-of-files/将文件列表排序为创建日期。但是,当我使用此代码运行脚本时:

import os

path = 'pages/'

files = sorted(os.listdir(path), key=os.path.getctime)

input(files)
Run Code Online (Sandbox Code Playgroud)

...我收到此错误:

Traceback (most recent call last):
  File "C:\ilmiont_server\blog\homepage.py", line 17, in <module>
    files = sorted(os.listdir(path), key=os.path.getctime)
  File "C:\Python33\lib\genericpath.py", line 64, in getctime
    return os.stat(filename).st_ctime
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'rsr.txt'
Run Code Online (Sandbox Code Playgroud)

“rsr.txt”目前是我搜索的“pages”目录中的唯一文件。奇怪的是它在与脚本相同的目录中工作,并且如果“页面”目录为空。对于上下文,我要排序的“页面”文件夹比脚本所在的文件夹低一级。请帮助我了解发生了什么问题!

提前致谢,Ilmiont。

python sorting date file

1
推荐指数
1
解决办法
2174
查看次数

按字母数字排序对哈希键进行排序

我刚刚阅读了在 Perl 中排序字母数字哈希键的帖子?. 但是我是从 Perl 开始的,我不是很清楚。

所以我有一个这样的哈希:

  %hash = (
        "chr1" => 1,
        "chr2" => 3,
        "chr19" => 14,
        "chr22" => 1,
        "X" => 2,
    )
Run Code Online (Sandbox Code Playgroud)

我试图获得这样的输出:

chr1
chr2
chr19
chr22
Run Code Online (Sandbox Code Playgroud)

但我正在获得这样的输出:

chr1
chr19
chr2
chr22
Run Code Online (Sandbox Code Playgroud)

我写了这段代码,但它创建了上面的错误输出:

foreach my $chr (sort {$a cmp $b} keys(%hash)) {
    my $total= $hash{$chr};
    my $differentpercent= ($differenthash{$chr} / $total)*100;
    my $round=(int($differentpercent*1000))/1000;
    print "$chr\t$hash{$chr}\t$differenthash{$chr}\t$round\n";
}
Run Code Online (Sandbox Code Playgroud)

它打印:

chr1    342421    7449    2.175
chr10    227648    5327    2.34
chr11    220415    4468    2.027
chr12    213263    4578    2.146 …
Run Code Online (Sandbox Code Playgroud)

sorting perl hash alphanumeric

1
推荐指数
1
解决办法
6396
查看次数

按字典键的整数对字典进行排序

假设我有一本这样的字典:

thedict={'1':'the','2':2,'3':'five','10':'orange'}
Run Code Online (Sandbox Code Playgroud)

我想按键对这本字典进行排序。如果我执行以下操作:

for key,value in sorted(thedict.iteritems()):
     print key,value
Run Code Online (Sandbox Code Playgroud)

我会得到

1 the
10 orange
2 2
3 five
Run Code Online (Sandbox Code Playgroud)

因为键是字符串而不是整数。我想对它们进行排序,就好像它们是整数一样,因此条目“10,orange”排在最后。我认为这样的事情会起作用:

for key,value in sorted(thedict.iteritems(),key=int(operator.itemgetter(0))):
    print key,value
Run Code Online (Sandbox Code Playgroud)

但这产生了这个错误:

TypeError: int() argument must be a string or a number, not 'operator.itemgetter'
Run Code Online (Sandbox Code Playgroud)

我在这里做错了什么?谢谢!

python sorting dictionary

1
推荐指数
1
解决办法
2145
查看次数