如何在数组内执行双重排序?(How to perform double sort inside an array?)
我不知道这种类型的排序存在确切的术语。 这是问题 - 我有一堂课
foo
class foo: def __init__(self,a1,a2): self.attrb1 = a1 self.attrb2 = a2 def sort(self): return self.attrb1
数组“bar”包含foo类型的对象。 我想根据这两个属性按降序对数组进行排序。 首先是
attrb1
bar.sort(key=foo.sort,reverse=True)
然后我想根据
attrb2
自己内部的排序元素进行排序。 因此,对于阵列中的两个元素foo1和foo2,我们有 -foo1 > foo2 if foo1.attrb1 > foo2.attrb1 elif foo1.attrb1 == foo2.attrb1 foo1.attrb2 > foo2.attrb2
我怎样才能做到这一点?
I don't know the exact term exists for this type of sorting. Here is the problem - I have a class
foo
class foo: def __init__(self,a1,a2): self.attrb1 = a1 self.attrb2 = a2 def sort(self): return self.attrb1
An array "bar" contain objects of type foo. I want to sort the array in descending order according to the two attributes. First by
attrb1
bar.sort(key=foo.sort,reverse=True)
Then I want to sort the sorted elements within themselves according to
attrb2
. So for two elements foo1 and foo2 in the array we have -foo1 > foo2 if foo1.attrb1 > foo2.attrb1 elif foo1.attrb1 == foo2.attrb1 foo1.attrb2 > foo2.attrb2
How can I do this?
原文:https://stackoverflow.com/questions/4768151
最满意答案
使用C ++ 11,你可以通过
explicit operator bool
:explicit operator bool() const { return !!*this; }
如果您需要明确地将对象转换为
bool
(由if
语句自动完成),则调用此运算符。 该实现通过调用您的operator !
在接收器对象上,然后返回相反的结果。希望这可以帮助!
Using C++11, you can do this by having an
explicit operator bool
:explicit operator bool() const { return !!*this; }
This operator is called if you ever need to cast your object to a
bool
explicitly (which is done by theif
statement automatically). The implementation works by calling youroperator !
on the receiver object, then returning the opposite result.Hope this helps!
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
在这种情况下,我会严格区分EF和NH,并且我不会在同一个问题中包含这两种技术。 简单的NH更成熟,具有可以更容易测试的代码。 同样在NH的情况下,您可以简单地将数据库切换到另一个数据库(如SQLite),并且在EF切换数据库可导致测试完全不同的应用程序的情况下,它的工作原理仍然是一样的,尤其是如果使用EF您可以在MS和非MS数据库之间切换。 什么是存储库? 让我们看看马丁福勒的定义 : 存储库在域和数据映射层之间进行中介,就像内存域对象集合一样。 客户端对象以声明方式构造查询规范并将其提交给Reposito ...
-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
我能够找到我想要的东西http://jmockit.org/api1x/mockit/Tested.html#availableDuringSetup-- I was able to find what i was looking for http://jmockit.org/api1x/mockit/Tested.html#availableDuringSetup--
-
您应该尝试Mighty Moose aka ContinuousTests它实际上会警告您未经测试的代码,并且是唯一免费的.NET代码覆盖工具。 You should try Mighty Moose a.k.a. ContinuousTests it actually warns you about untested code and is the only code coverage tool for .NET that is free.
-
至于更改SQL语句,您可以这样做(使用各种JOINS ): SELECT @JunitSlugs = ISNULL(@JunitSlugs, '') + ',' + jl.JunitSlug + '-' + lu.LookupValue FROM JunitList jl INNER JOIN Lookup lu ON jl.JunitTypeID = lu.LookupID WHERE jl.JunitId IN (SELECT JunitId FROM Caps ...
-
为了展示正确的思考方式,正如评论中所述 - 这里的技术是识别您重复相同的代码片段三次,并且这三个中的两个未被正确检查。 它通常被称为DRY原则(“不要重复自己”)。 注意到这一点,你重构你的代码,以便“它是干的”。 DRY代码比非DRY代码具有更少的故障点,这显然降低了出错的可能性。 int previous = 1, current = 1; int next; int sum = 0; // single test for end while (current < 4000000) { // ...
-
始终引用参数扩展。 fg () { if [ -z "$1" ] && [ -z "$2" ]; then echo "Help: must specify path and regular expression" else find "$1" -regex "$2" fi } fg 但是,检查未设置或空参数的更简洁方法是:? 运营商。 fg () { : ${1:?Missing path} : ${2:?Missing regex} find " ...
-
如何生成在被测试的类中创建的模拟对象?(How to generate a mocked object that is created within the class being tested?)[2023-04-13]
答案是将EXPECT_CALL()放入构造函数中,使用*this作为第一个参数。 当然,这只有在您希望它始终保持相同时才有效。 如果预期的行为在不同实例之间变化,这将变得更加复杂,除非每个实例使用不同的构造函数。 如果使用相同的构造函数,那么您的模拟类可以使用静态计数器,该计数器可以根据实例计数更改预期值。 The answer is to put the EXPECT_CALL() into the constructor, using *this as the first parameter. Of c ... -
使用C ++ 11,你可以通过explicit operator bool : explicit operator bool() const { return !!*this; } 如果您需要明确地将对象转换为bool (由if语句自动完成),则调用此运算符。 该实现通过调用您的operator ! 在接收器对象上,然后返回相反的结果。 希望这可以帮助! Using C++11, you can do this by having an explicit operator bool: explic ...