
1 ipconfiger 2012 年 10 月 20 日 python: ls.sort(lambda x,y:cmp(x["a"]*10+x["b"],y["a"]*10+y["b"])) |
2 paulguo OP Javascript: arr.sort(function(x,y){return y.a-x.a||x.b-y.b}); |
3 fwee 2012 年 10 月 20 日 a.sort{|a,b|r = b[:a] <=> a[:a]; r == 0 ? (a[:b] <=> b[:b]) : r} 顺便说下1L算法错了 |
4 ipconfiger 2012 年 10 月 20 日 @fwee 哟西,没看到B生序 |
5 paulguo OP 1L 的确不对,LS什么语言? |
6 fwee 2012 年 10 月 20 日 3# Ruby 忘说了 |
7 clowwindy 2012 年 10 月 20 日 coffee arr.sort (x, y) -> y.a - x.a || x.b - y.b |
8 imcotton 2012 年 10 月 20 日 ActionScript 3 arr.sortOn(["a", "b"], [Array.NUMERIC | Array.DESCENDING, Array.NUMERIC]) |
9 binux 2012 年 10 月 20 日 python: sorted(the_list, key=lambda x: (x['a'], -x['b'])) |
10 mckelvin 2012 年 10 月 21 日 ls真乃神人也 |
11 fwee 2012 年 10 月 21 日 ruby a.sort_by{|i| [-i[:a],i[:b]]} 这是最短的了吧 |
12 Keinez 2012 年 10 月 21 日 9楼和11楼好棒!(咱外行居然也看懂了QAQ) |
13 darkfall 2012 年 10 月 21 日 C++11 std::sort(arr.begin(), arr.end(), [](Dict& a, Dict& b) -> bool { return (a['a'] == b['a']) ? (a['b'] < b['b']) : (a['a'] > b['a']); }); |
14 shiny PRO 追求短小的话,PHP 可以是: usort($items,function($a, $b){ return $a['a']==$b['a'] ? bccomp($a['b'],$b['b']) : bccomp($b['a'],$a['a']); }); 但是后面谁来维护这程序可能会骂了…… 所以正常项目中我会这么写: //兼容个别未开启 bcmath 的环境 if(!function_exists('bccomp')){ function bccomp($a,$b){ if($a==$b){ return 0; } else if($a>$b){ return 1; } else { return -1; } } } function sort_items($a, $b){ if($b['a']==$a['a']){ return bccomp($a['b'],$b['b']); } else { return bccomp($b['a'],$a['a']); } } |
15 luin 2012 年 10 月 21 日 喜欢CoffeeScript,多易读啊 |
16 liuxurong 2012 年 10 月 21 日 喜欢Python,多易读啊 |
17 insub 2012 年 10 月 21 日 喜欢ruby,最易读了啊.......... |
18 alsotang 2012 年 10 月 21 日 CoffeeScript +1 |
19 nowgoo 2012 年 10 月 21 日 SQL: SELECT a, b FROM t ORDER BY a DESC, b ASC; |
20 blacktulip 2012 年 10 月 21 日 看了半天还是SQL最易读,一看就懂 |
21 haxe 2012 年 10 月 22 日 haxe语言实现...权当抛砖引玉,因为本语言排序上没有上面那么强大的原生api,所以手写一个比较函数。为了性能上考虑避免运行时类型检查,定义了这个数组中元素的结构体A,顺便还提供了代码提示功能为维护提供了便利。 typedef A = { var a : Int; var b : Int; } //代码实现 var arr = [ // type inference {a: 5, b: 5}, {a: 6, b: 2}, {a: 2, b: 7}, {a: 5, b: 2}, {a: 1, b: 0}, {a: 5, b: 1}, {a: 6, b: 1}, {a: 2, b: 9} ]; arr.sort( function( x:A , y:A ):Int { if ( x.a > y.a ) return -1; else if ( x.a == y.a ) if ( x.b > y.b) return 1; else if ( x.b == y.b ) return 0; else return -1; else return 1; } ); |
22 clowwindy 2012 年 10 月 22 日 既然出现了 SQL,那我贴两个有意思的: C# var result = list.OrderByDescending(x => x["a"]).ThenBy(x => x["b"]); C# with Linq var result = from i in list orderby i["a"] descending, i["b"] select i; C# 这么有趣的语法特性可惜被微软给糟蹋了。 |
24 cloudream 2012 年 10 月 22 日 list.sortBy( x => ( -x("a"), x("b") ) ) Scala |
25 dndx 2012 年 10 月 22 日 C版本,也就20来行。 http://gist.github.com/3929214 |
26 |
27 chisj 2012 年 10 月 23 日 对不起,请无视上一条回复,第一次用Gist太兴奋复制错了。 http://gist.github.com/3936202 |
28 tioover 2012 年 10 月 23 日 壮哉我大Python |
29 jesse0628 2012 年 10 月 29 日 来一段common lisp吧~ (sort lst #'> :key #'(lambda (x) (+ (* 10 (getf x :a)) (- 9 (getf x :b))))) |
32 ylem 2012 年 10 月 30 日 |
33 ylem 2012 年 10 月 30 日 呃。。。不太会用gist。。。直接靠代码: NSArray *arr = [SArray arrayWithObjects: [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:5], @"a", [NSNumber numberWithInt:5], @"b",nil], [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:6], @"a", [NSNumber numberWithInt:2], @"b",nil], [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:2], @"a", [NSNumber numberWithInt:7], @"b",nil], [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:5], @"a", [NSNumber numberWithInt:2], @"b",nil], [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1], @"a", [NSNumber numberWithInt:0], @"b",nil], [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:5], @"a", [NSNumber numberWithInt:1], @"b",nil], [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:6], @"a", [NSNumber numberWithInt:1], @"b",nil], [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:2], @"a", [NSNumber numberWithInt:9], @"b",nil], nil]; NSSortDescriptor *ageDescriptor = [[NSSortDescriptor alloc] initWithKey:@"a" ascending:NO]; NSSortDescriptor *hireDateDescriptor = [[NSSortDescriptor alloc] initWithKey:@"b" ascending:YES]; NSArray *sortDescriptors = @[ageDescriptor, hireDateDescriptor]; NSArray *sortedArray = [arr sortedArrayUsingDescriptors:sortDescriptors]; NSLog(@"after sort the arr is:%@", sortedArray); |
34 ylem 2012 年 10 月 30 日 另,回复怎么插入html代码? |
35 ylem 2012 年 10 月 30 日 |
36 ylem 2012 年 10 月 30 日 。。。。。 |
37 ylem 2012 年 10 月 30 日 |
38 ylem 2012 年 10 月 30 日 好吧。。大家继续。。。 |
39 lwjefSub 2012 年 10 月 30 日 |
40 lwjefSub 2012 年 10 月 30 日 去掉s |
41 ylem 2012 年 10 月 30 日 |
42 chisj 2012 年 10 月 30 日 @yinsigan 是的,那个block就是算法,单词确实要打很多,所以不可能用vim之类编辑器来写代码,用xcode的话会有很好的代码提醒,但是函数名不适合记忆。 当然有时候选择少了也是一种幸福啊。 |
43 plprapper 2012 年 10 月 30 日 shell sort -k2nr -k4n |
44 egen 2012 年 10 月 30 日 目测最易懂的是sql,最糟糕的是objc |
45 andy12530 2012 年 10 月 30 日 2L用原生函数,壮载我大JS |
46 infinte 2012 年 10 月 30 日 moescript list.soty (x, y) => (x.a - y.a) or (x.b - y.b) |
47 infinte 2012 年 10 月 30 日 抱歉错个字…… |
48 jiyinyiyong 2012 年 10 月 30 日 `a` 赋值了那个数组的话: ```livescript a |> (.sort -> &0.b - &1.b) |> (.sort -> &1.a - &0.a) |> console.log ``` |
49 Cwind 2012 年 10 月 30 日 objc声明array用新语法可以简洁点: NSArray *array = @[@{@"a":@5,@"b":@5},@{@"a":@6,@"b":@2},...]; |
50 moonranger 2012 年 10 月 30 日 来个Clojure版: (def data [{:a 5 :b 5} {:a 6 :b 2} {:a 2 :b 7} {:a 5 :b 2} {:a 1 :b 0} {:a 5 :b 1} {:a 6 :b 1} {:a 2 :b 9}]) (println (sort-by #(+ (* -10 (:a %)) (:b %)) data)) |
51 moonranger 2012 年 10 月 30 日 P.S. 向所有Java程序员推荐Clojure |
52 powerx1202 2012 年 10 月 31 日 在学haskell,写个naive的。。 List.sortBy (\x y -> let xa = fromJust $ Map.lookup 'a' x; xb = fromJust $ Map.lookup 'b' x; ya = fromJust $ Map.lookup 'a' y; yb = fromJust $ Map.lookup 'b' y in if xa == ya then xb `compare` yb else ya `compare` xa) ar |
53 levn 2012 年 11 月 1 日 @powerx1202 那我也写个…… sortThat = sortBy $ ((flip compare) `on` (! "a")) `mappend` (compare `on` (! "b")) |
54 powerx1202 2012 年 11 月 1 日 @levn 哈,诱出高手了,我才刚看到monoid。。 |
55 hpyhacking 2012 年 11 月 1 日 compare({_A, B1}, {_A, B2}) -> B2 > B1; compare({A1, _B1}, {A2, _B2}) -> A1 > A2. lists:sort(fun compare/2, List). |
56 silverbullettt 2012 年 11 月 1 日 没人用 Scheme? (define lst (list (s 5 5) (s 6 2) (s 2 7) (s 5 2) (s 1 0) (s 5 1) (s 6 1) (s 2 9))) (define-struct s (a b)) (sort lst (lambda (x y) (if (= (s-a x) (s-a y)) (< (s-b x) (s-b y)) (> (s-a x) (s-a y)))))) 其实这是 Racket~ |
57 silverbullettt 2012 年 11 月 1 日 我去,缩进被吃了 |