$76 GRAYBYTE WORDPRESS FILE MANAGER $17

SERVER : premium201.web-hosting.com #1 SMP Wed Mar 26 12:08:09 UTC 2025
SERVER IP : 172.67.217.254 | ADMIN IP 216.73.216.157
OPTIONS : CRL = ON | WGT = ON | SDO = OFF | PKEX = OFF
DEACTIVATED : NONE

/opt/alt/ruby18/share/ri/1.8/system/Enumerable/

HOME
Current File : /opt/alt/ruby18/share/ri/1.8/system/Enumerable//sort_by-i.yaml
--- !ruby/object:RI::MethodDescription 
aliases: []

block_params: 
comment: 
- !ruby/struct:SM::Flow::P 
  body: Sorts <em>enum</em> using a set of keys generated by mapping the values in <em>enum</em> through the given block.
- !ruby/struct:SM::Flow::VERB 
  body: "   %w{ apple pear fig }.sort_by {|word| word.length}\n                #=&gt; [&quot;fig&quot;, &quot;pear&quot;, &quot;apple&quot;]\n"
- !ruby/struct:SM::Flow::P 
  body: The current implementation of <tt>sort_by</tt> generates an array of tuples containing the original collection element and the mapped value. This makes <tt>sort_by</tt> fairly expensive when the keysets are simple
- !ruby/struct:SM::Flow::VERB 
  body: "   require 'benchmark'\n   include Benchmark\n\n   a = (1..100000).map {rand(100000)}\n\n   bm(10) do |b|\n     b.report(&quot;Sort&quot;)    { a.sort }\n     b.report(&quot;Sort by&quot;) { a.sort_by {|a| a} }\n   end\n"
- !ruby/struct:SM::Flow::P 
  body: <em>produces:</em>
- !ruby/struct:SM::Flow::VERB 
  body: "   user     system      total        real\n   Sort        0.180000   0.000000   0.180000 (  0.175469)\n   Sort by     1.980000   0.040000   2.020000 (  2.013586)\n"
- !ruby/struct:SM::Flow::P 
  body: However, consider the case where comparing the keys is a non-trivial operation. The following code sorts some files on modification time using the basic <tt>sort</tt> method.
- !ruby/struct:SM::Flow::VERB 
  body: "   files = Dir[&quot;*&quot;]\n   sorted = files.sort {|a,b| File.new(a).mtime &lt;=&gt; File.new(b).mtime}\n   sorted   #=&gt; [&quot;mon&quot;, &quot;tues&quot;, &quot;wed&quot;, &quot;thurs&quot;]\n"
- !ruby/struct:SM::Flow::P 
  body: "This sort is inefficient: it generates two new <tt>File</tt> objects during every comparison. A slightly better technique is to use the <tt>Kernel#test</tt> method to generate the modification times directly."
- !ruby/struct:SM::Flow::VERB 
  body: "   files = Dir[&quot;*&quot;]\n   sorted = files.sort { |a,b|\n     test(?M, a) &lt;=&gt; test(?M, b)\n   }\n   sorted   #=&gt; [&quot;mon&quot;, &quot;tues&quot;, &quot;wed&quot;, &quot;thurs&quot;]\n"
- !ruby/struct:SM::Flow::P 
  body: This still generates many unnecessary <tt>Time</tt> objects. A more efficient technique is to cache the sort keys (modification times in this case) before the sort. Perl users often call this approach a Schwartzian Transform, after Randal Schwartz. We construct a temporary array, where each element is an array containing our sort key along with the filename. We sort this array, and then extract the filename from the result.
- !ruby/struct:SM::Flow::VERB 
  body: "   sorted = Dir[&quot;*&quot;].collect { |f|\n      [test(?M, f), f]\n   }.sort.collect { |f| f[1] }\n   sorted   #=&gt; [&quot;mon&quot;, &quot;tues&quot;, &quot;wed&quot;, &quot;thurs&quot;]\n"
- !ruby/struct:SM::Flow::P 
  body: This is exactly what <tt>sort_by</tt> does internally.
- !ruby/struct:SM::Flow::VERB 
  body: "   sorted = Dir[&quot;*&quot;].sort_by {|f| test(?M, f)}\n   sorted   #=&gt; [&quot;mon&quot;, &quot;tues&quot;, &quot;wed&quot;, &quot;thurs&quot;]\n"
full_name: Enumerable#sort_by
is_singleton: false
name: sort_by
params: |
  enum.sort_by {| obj | block }    => array

visibility: public


Current_dir [ NOT WRITEABLE ] Document_root [ NOT WRITEABLE ]


[ Back ]
NAME
SIZE
LAST TOUCH
USER
CAN-I?
FUNCTIONS
..
--
3 Mar 2024 10.50 PM
root / linksafe
0755
Enumerator
--
3 Mar 2024 10.50 PM
root / linksafe
0755
all%3f-i.yaml
0.836 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
any%3f-i.yaml
0.856 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
cdesc-Enumerable.yaml
2.902 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
collect-i.yaml
0.595 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
count-i.yaml
0.796 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
cycle-i.yaml
0.875 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
detect-i.yaml
0.715 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
drop-i.yaml
0.425 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
drop_while-i.yaml
0.526 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
each_cons-i.yaml
0.618 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
each_slice-i.yaml
0.55 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
each_with_index-i.yaml
0.595 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
entries-i.yaml
0.533 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
enum_cons-i.yaml
0.618 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
enum_slice-i.yaml
0.55 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
enum_with_index-i.yaml
0.595 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
find-i.yaml
0.711 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
find_all-i.yaml
0.528 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
find_index-i.yaml
0.751 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
first-i.yaml
0.444 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
grep-i.yaml
0.825 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
group_by-i.yaml
0.507 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
include%3f-i.yaml
0.571 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
inject-i.yaml
0.204 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
map-i.yaml
0.587 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
max-i.yaml
0.662 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
max_by-i.yaml
0.455 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
member%3f-i.yaml
0.569 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
min-i.yaml
0.662 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
min_by-i.yaml
0.449 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
minmax-i.yaml
0.769 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
minmax_by-i.yaml
0.554 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
none%3f-i.yaml
0.874 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
one%3f-i.yaml
0.872 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
partition-i.yaml
0.524 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
reduce-i.yaml
2.052 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
reject-i.yaml
0.474 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
reverse_each-i.yaml
0.29 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
select-i.yaml
0.524 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
sort-i.yaml
0.904 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
sort_by-i.yaml
3.164 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
take-i.yaml
0.389 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
take_while-i.yaml
0.497 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
to_a-i.yaml
0.527 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
to_set-i.yaml
0.34 KB
26 Jul 2023 1.47 PM
root / linksafe
0644
zip-i.yaml
1.009 KB
26 Jul 2023 1.47 PM
root / linksafe
0644

GRAYBYTE WORDPRESS FILE MANAGER @ 2025 CONTACT ME
Static GIF