1
codegear 2015-04-24 11:43:18 +08:00
不知楼主的排序规则。大体上可以sort(l, key=...)
|
2
ledzep2 2015-04-24 11:43:19 +08:00
l.sort(key=lambda x:x['value'])
|
3
hahastudio 2015-04-24 11:46:06 +08:00
按什么排序?
list.sort = sort(...) L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*; cmp(x, y) -> -1, 0, 1 或者用 list(sorted( ... )) sorted(...) sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list 比如 >>> l.sort(key=lambda i:i['value']) >>> l [{'name': 'n1', 'value': 90}, {'name': 'n4', 'value': 90}, {'name': 'n0', 'value': 93}, {'name': 'n3', 'value': 93}, {'name': 'n2', 'value': 95}] |
4
cheerzeng OP |
5
hahastudio 2015-04-24 15:29:15 +08:00
@cheerzeng one line quick sort 也是经典的 snippet,虽然只能用来炫技
qsort = lambda l: [] if not l else qsort([x for x in l[1:] if x <= l[0]]) + [l[0]] + qsort([x for x in l[1:] if x > l[0]]) |
6
cheerzeng OP @hahastudio 这个我也看到了,是不是除了快速排序,就不可能用其他算法放一行代码里了?
|
7
hahastudio 2015-04-24 16:55:56 +08:00
@cheerzeng https://code.activestate.com/recipes/578909-one-liner-sort-method-modified-selection-sort/
一行除了炫技,真没什么用= = 极端点的话,Python 还是有 ; 的 |
8
cheerzeng OP @hahastudio 回头看看,谢谢啊~
|
9
pupboss 2015-04-24 17:15:31 +08:00
OC 做过类似的
``` NSArray *arr = [self.planArr sortedArrayUsingComparator:^NSComparisonResult(ExamPlan *obj1, ExamPlan *obj2) { NSComparisonResult result = [obj1.startTime compare:obj2.startTime]; return result; }]; ``` |