⚝
One Hat Cyber Team
⚝
Your IP:
216.73.216.93
Server IP:
65.108.141.171
Server:
Linux server.heloix.com 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
Server Software:
Apache
PHP Version:
7.4.33
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
usr
/
share
/
ri
/
2.7.0
/
system
/
Enumerable
/
Edit File: sort_by-i.ri
U:RDoc::AnyMethod[iI"sort_by:ETI"Enumerable#sort_by;TF:publico:RDoc::Markup::Document:@parts[!o:RDoc::Markup::Paragraph; [I"DSorts <i>enum</i> using a set of keys generated by mapping the ;TI"3values in <i>enum</i> through the given block.;To:RDoc::Markup::BlankLine o; ; [I"JThe result is not guaranteed to be stable. When two keys are equal, ;TI">the order of the corresponding elements is unpredictable.;T@o; ; [I"=If no block is given, an enumerator is returned instead.;T@o:RDoc::Markup::Verbatim; [I"7%w{apple pear fig}.sort_by { |word| word.length } ;TI"0 #=> ["fig", "pear", "apple"] ;T:@format0o; ; [ 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"<em>produces:</em>;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"]enum.sort_by { |obj| block } -> array enum.sort_by -> an_enumerator ;T0[ I"();T@fFI"Enumerable;TcRDoc::NormalModule00
Simpan