ÿØÿà JFIF    ÿÛ „  ( %!1!%)+//.383,7(-.+  -%%-////---/-.+/--+------/------/--0+--/-/-----.-----ÿÀ  ¥2" ÿÄ     ÿÄ J    ! 1AQ"aq2‘#BR‚¡ÁÑ3br’¢±Âð$CSƒ²á4c“%DsÓñÿÄ   ÿÄ *  !1AQa‘"2q3±ð#b¡ÿÚ   ? ¼QxJQaÍuò¸Zö Úü8,ÐÚú "SSn<rçù–´âE—^ªBÖ9À\†¸ÔÁT­ÃÛ5 ëd´³Í#Ý;Þ38œî ¶H£M:wÎ3…³…âpÔF&‚FK¸9„â4àGEõªfÿ ‘ñ(ßw­pŽF|È¥ù®häðÍѶ¹‘[ÒinÙW¶ùñY˜Q{›K"išÒ[Ú8žë\F¹@-?v"ÔU”,ìöžkÿ {I‡£šÍ?e ríV ?> ......................................... ............................................................................. ÿØÿà JFIF    ÿÛ „  ( %!1!%)+//.383,7(-.+  -%%-////---/-.+/--+------/------/--0+--/-/-----.-----ÿÀ  ¥2" ÿÄ     ÿÄ J    ! 1AQ"aq2‘#BR‚¡ÁÑ3br’¢±Âð$CSƒ²á4c“%DsÓñÿÄ   ÿÄ *  !1AQa‘"2q3±ð#b¡ÿÚ   ? ¼QxJQaÍuò¸Zö Úü8,ÐÚú "SSn<rçù–´âE—^ªBÖ9À\†¸ÔÁT­ÃÛ5 ëd´³Í#Ý;Þ38œî ¶H£M:wÎ3…³…âpÔF&‚FK¸9„â4àGEõªfÿ ‘ñ(ßw­pŽF|È¥ù®häðÍѶ¹‘[ÒinÙW¶ùñY˜Q{›K"išÒ[Ú8žë\F¹@-?v"ÔU”,ìöžkÿ {I‡£šÍ?e ríV ?> ......................................... ............................................................................. ???????????????????????????????????? ???????????????????????????????????? ÿØÿà JFIF    ÿÛ „  ( %!1!%)+//.383,7(-.+  -%%-////---/-.+/--+------/------/--0+--/-/-----.-----ÿÀ  ¥2" ÿÄ     ÿÄ J    ! 1AQ"aq2‘#BR‚¡ÁÑ3br’¢±Âð$CSƒ²á4c“%DsÓñÿÄ   ÿÄ *  !1AQa‘"2q3±ð#b¡ÿÚ   ? ¼QxJQaÍuò¸Zö Úü8,ÐÚú "SSn<rçù–´âE—^ªBÖ9À\†¸ÔÁT­ÃÛ5 ëd´³Í#Ý;Þ38œî ¶H£M:wÎ3…³…âpÔF&‚FK¸9„â4àGEõªfÿ ‘ñ(ßw­pŽF|È¥ù®häðÍѶ¹‘[ÒinÙW¶ùñY˜Q{›K"išÒ[Ú8žë\F¹@-?v"ÔU”,ìöžkÿ {I‡£šÍ?e ríV ?> ......................................... ............................................................................. ÿØÿà JFIF    ÿÛ „  ( %!1!%)+//.383,7(-.+  -%%-////---/-.+/--+------/------/--0+--/-/-----.-----ÿÀ  ¥2" ÿÄ     ÿÄ J    ! 1AQ"aq2‘#BR‚¡ÁÑ3br’¢±Âð$CSƒ²á4c“%DsÓñÿÄ   ÿÄ *  !1AQa‘"2q3±ð#b¡ÿÚ   ? ¼QxJQaÍuò¸Zö Úü8,ÐÚú "SSn<rçù–´âE—^ªBÖ9À\†¸ÔÁT­ÃÛ5 ëd´³Í#Ý;Þ38œî ¶H£M:wÎ3…³…âpÔF&‚FK¸9„â4àGEõªfÿ ‘ñ(ßw­pŽF|È¥ù®häðÍѶ¹‘[ÒinÙW¶ùñY˜Q{›K"išÒ[Ú8žë\F¹@-?v"ÔU”,ìöžkÿ {I‡£šÍ?e ríV ?> ......................................... ............................................................................. ???????????????????????????????????? ???????????????????????????????????? U:RDoc::AnyMethod[iI" sort_by:ETI"Enumerable#sort_by;TF: publico:RDoc::Markup::Document: @parts[!o:RDoc::Markup::Paragraph; [I"AWith a block given, returns an array of elements of +self+, ;TI"Ksorted according to the value returned by the block for each element. ;TI"IThe ordering of equal elements is indeterminate and may be unstable.;To:RDoc::Markup::BlankLineo; ; [I"Examples:;T@o:RDoc::Markup::Verbatim; [ I"a = %w[xx xxx x xxxx] ;TI"Da.sort_by {|s| s.size } # => ["x", "xx", "xxx", "xxxx"] ;TI"Da.sort_by {|s| -s.size } # => ["xxxx", "xxx", "xx", "x"] ;TI""h = {foo: 2, bar: 1, baz: 0} ;TI"Kh.sort_by{|key, value| value } # => [[:baz, 0], [:bar, 1], [:foo, 2]] ;TI"Kh.sort_by{|key, value| key } # => [[:bar, 1], [:baz, 0], [:foo, 2]] ;T: @format0o; ; [I"0With no block given, returns an Enumerator.;T@o; ; [ I"BThe current implementation of #sort_by generates an array of ;TI"Ftuples containing the original collection element and the mapped ;TI"Fvalue. This makes #sort_by fairly expensive when the keysets are ;TI" simple.;T@o; ; [ I"require 'benchmark' ;TI" ;TI"*a = (1..100000).map { rand(100000) } ;TI" ;TI"Benchmark.bm(10) do |b| ;TI"& b.report("Sort") { a.sort } ;TI"3 b.report("Sort by") { a.sort_by { |a| a } } ;TI" end ;T; 0o; ; [I"produces:;T@o; ; [I",user system total real ;TI"=Sort 0.180000 0.000000 0.180000 ( 0.175469) ;TI"=Sort by 1.980000 0.040000 2.020000 ( 2.013586) ;T; 0o; ; [I"JHowever, consider the case where comparing the keys is a non-trivial ;TI"Ioperation. The following code sorts some files on modification time ;TI""using the basic #sort method.;T@o; ; [I"files = Dir["*"] ;TI"Lsorted = files.sort { |a, b| File.new(a).mtime <=> File.new(b).mtime } ;TI"2sorted #=> ["mon", "tues", "wed", "thurs"] ;T; 0o; ; [ I"9This sort is inefficient: it generates two new File ;TI"Hobjects during every comparison. A slightly better technique is to ;TI"=use the Kernel#test method to generate the modification ;TI"times directly.;T@o; ; [ I"files = Dir["*"] ;TI""sorted = files.sort { |a, b| ;TI"# test(?M, a) <=> test(?M, b) ;TI"} ;TI"2sorted #=> ["mon", "tues", "wed", "thurs"] ;T; 0o; ; [ I"@This still generates many unnecessary Time objects. A more ;TI"Gefficient technique is to cache the sort keys (modification times ;TI"Hin this case) before the sort. Perl users often call this approach ;TI"Da Schwartzian transform, after Randal Schwartz. We construct a ;TI"Dtemporary array, where each element is an array containing our ;TI"Dsort key along with the filename. We sort this array, and then ;TI"*extract the filename from the result.;T@o; ; [ I"%sorted = Dir["*"].collect { |f| ;TI" [test(?M, f), f] ;TI"!}.sort.collect { |f| f[1] } ;TI"2sorted #=> ["mon", "tues", "wed", "thurs"] ;T; 0o; ; [I"3This is exactly what #sort_by does internally.;T@o; ; [I"3sorted = Dir["*"].sort_by { |f| test(?M, f) } ;TI"2sorted #=> ["mon", "tues", "wed", "thurs"] ;T; 0o; ; [I"KTo produce the reverse of a specific order, the following can be used:;T@o; ; [I"!ary.sort_by { ... }.reverse!;T; 0: @fileI" enum.c;T:0@omit_headings_from_table_of_contents_below0I"Nsort_by {|element| ... } -> array sort_by -> enumerator ;T0[I"();T@jFI"Enumerable;TcRDoc::NormalModule00