Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From your question it is unclear to me how you really want to sort and how you produced the examples. I cannot detect any order in the example of your expected sort order. A likely solution is at the bottom.</p> <p>Let me clarify:</p> <p>Given a textfile "ts" with the following content (your example):</p> <pre><code>&gt; cat ts 21:01:2011 16:51:09 21:01:2011 16:49:54 26:01:2011 11:02:55 26:01:2011 11:01:40 05:04:2011 11:51:13 05:04:2011 11:51:13 05:04:2011 11:48:37 05:04:2011 11:48:37 </code></pre> <p>A standard sort produces the following output:</p> <pre><code>&gt; perl -e '@a = &lt;&gt;; print sort @a' ts 05:04:2011 11:48:37 05:04:2011 11:48:37 05:04:2011 11:51:13 05:04:2011 11:51:13 21:01:2011 16:49:54 21:01:2011 16:51:09 26:01:2011 11:01:40 26:01:2011 11:02:55 </code></pre> <p>While the numerically descending sort you proposed produces the following order:</p> <pre><code>&gt; perl -e '@a = &lt;&gt;; print sort { $b &lt;=&gt; $a } @a' ts 26:01:2011 11:02:55 26:01:2011 11:01:40 21:01:2011 16:51:09 21:01:2011 16:49:54 05:04:2011 11:51:13 05:04:2011 11:51:13 05:04:2011 11:48:37 05:04:2011 11:48:37 </code></pre> <p>To clarify on the numerical sort: The spaceship operator &lt;=> enforces numerical interpretation of its two operands. So the strings $a and $b, each containing the date and time, are interpreted as if they were numbers. To do this perl in this example extracts the date and stops at the first ':'. That's why the time, and even the month and year are completely ignored and we're only sorting for the day of the month in descending order.</p> <p>Finally, if you really want to reverse sort for date, then time and need to keep the format you can use this code:</p> <pre><code>&gt; perl -e '@a = &lt;&gt;; sub dmyt2ymdt { my $dmyt=shift; $ymdt=join(q(), (split(/[:\s]+/,$dmyt))[2,1,0,3,4,5])} print sort { dmyt2ymdt($b) &lt;=&gt; dmyt2ymdt($a) } @a' ts 05:04:2011 11:51:13 05:04:2011 11:51:13 05:04:2011 11:48:37 05:04:2011 11:48:37 26:01:2011 11:02:55 26:01:2011 11:01:40 21:01:2011 16:51:09 21:01:2011 16:49:54 </code></pre> <p>Here's a nicer formatted version (which I did not test):</p> <pre><code>sub dmyt2ymdt { my $dmyt = shift; my ($day, $mon, $year, $h, $m, $s) = split(/[:\s]+/, $dmyt); return join('', $year, $mon, $day, $h, $m, $s); } </code></pre> <p>This sort function</p> <pre><code>sort { dmyt2ymdt($b) &lt;=&gt; dmyt2ymdt($a) } </code></pre> <p>then calls the above helper quite a lot. In your example we have 8 entries in the list to sort and the function gets called 24 times. So it is not performance efficient. But for small lists up to a couple hundred or even thousand entries it may be alright for you. If you have large lists, you should do the format conversion only once, but it still costs memory. So for large lists, you need to tradeoff memory versus execution time, as is often the case.</p> <p>IF performance is the optimization criteria, you could do the transformation on the fly as has been commented and shown in other answers and comments like this:</p> <pre><code>sort { $b &lt;=&gt; $a } map { dmyt2ymdt($_) } @a </code></pre> <p>..for my example above. Now you do the conversion only once per element. Still, we have to hold a temporary list in memory. I'm not exactly sure how well perl could optimize the above construct. One may think that the following is easier to optimize:</p> <pre><code>reverse sort map { dmyt2ymdt($_) } @a </code></pre> <p>which would work for the testset, too. The sort defaults back to the string comparison which is the same as a numerical comparison for strings of identical length which do not use spaces in those locations where other strings have digits.</p> <p>Hope this helps!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload