如何使这段代码按降序排列。这段代码总是按升序给我数组:
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)
有任何想法吗?
每当我们执行 db.Collection.find().sort() 时,只有我们的输出被排序,而不是集合本身,
即如果我做 db.collection.find() 然后我看到原始集合,而不是排序的集合。
有没有办法对集合本身进行排序,而不仅仅是对输出进行排序?
将排序结果导出到整个新集合中也可以。
如果我对 _id 字段进行了编号。(例如 _id:1 、 _id_2 、 _id:3 等等)
我想要一个批处理文件,它将按文件类型对文件进行排序并将它们分类到文件夹中。例如,我将在某个文件夹中运行此批处理文件,而 .PDF 文件将保存在“PDF”文件夹中,与其他文件类型相同。可以在命令行中做到这一点吗?谢谢你。
如何根据python中单词中的元音数对列表进行排序?
我没有在任何网站上找到答案。这个词应该按照元音数量降序排列。
我编写了以下实现 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) 我有一个包含两列数据的表格。当我用列 [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) 下面的代码在排序方面有什么问题?排序代码被命中,但排序从未应用于结果。
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) 我一直在关注本教程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。
我刚刚阅读了在 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) 假设我有一本这样的字典:
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)
我在这里做错了什么?谢谢!
sorting ×10
python ×3
alphanumeric ×1
arraylist ×1
batch-file ×1
c# ×1
cmd ×1
compare ×1
d3.js ×1
date ×1
dictionary ×1
file ×1
file-type ×1
hash ×1
icomparer ×1
ios ×1
iqueryable ×1
javascript ×1
linq ×1
list ×1
mongodb ×1
nsarray ×1
perl ×1
python-2.7 ×1