Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7406987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:38:25+00:00 2026-05-29T05:38:25+00:00

I’m attempting to implement the Tcl dictionary sort in Perl in order to order

  • 0

I’m attempting to implement the Tcl dictionary sort in Perl in order to order some files. For those who don’t know Tcl, you can get it to sort continuous integers by their value, it’s detailed here:

http://www.perlmonks.org/index.pl?node_id=160157

To summarise:
A given array of:

qw(
  bigbang
  x10y
  x9y
  bigboy
  bigBoy
  x11y
)

is sorted by case-insensitive letter, then case sensitive as a tie-breaker, then by the number, except it takes any following numbers and interprets the entire thing as a single number in the sort, so the above comes out as:

qw(
    bigbang
    bigBoy
    bigboy
    x9y
    x10y
    x11y
)

With x9y appearing above x10y and x11y, whilst in a standard ASCII sort, x10y and x11y would come above x9y, due to 1 appearing before 9.

I attempted to implement Juerd’s example in that link as a function, but in my case, the sort perfectly mimics a Tcl dictionary sort when I have a list of version numbers, like the following:

qw{ 1 1.0 1.01 1.2 1.02 1.0003 1.102 1.103 1.203 102a 102b 103a 103b 123 };

But when absolute paths are used for the files, the ordering messes up.

I’ve posted an example script below. If anyone can see why the function is going wrong, or if you can suggest an more modern alternative (since the example I worked from was posted 10 years ago :P), I would appreciate it.

http://pastebin.com/WM6QhzSK

And if you want to see a Tcl dictionary sort in action, check the link below:

http://pastebin.com/h3qMT4C2

Thanks in advance!

EDIT:- Thanks to choroba for leading me to the solution! The working function is as follows:

sub dict_sort {
  my @unsorted = @_;
  my @sorted =
    map $_->[0],
    sort {
      my $i = 0;
      {
        my $A = $a->[1][$i];
        my $B = $b->[1][$i];
        defined($A) || defined($B)       # Stop if both undef
        and (
          defined($A) <=> defined($B)  # Defined wins over undef
          or (
            $A !~ /\d/ || $B !~ /\d/ # $A or $B is non-integer
            ?    (lc $A cmp lc $B)   # ?? Stringy lowercase
              || (   $A cmp    $B)   #    -> Tie breaker
            : $A <=> $B              # :: $A and $B are integers
              or (
                length($A) <=> length($B)  # If numeric comparison returns the same, check length to sort by leading zeroes
              )
          )
          or ++$i && redo              # tie => next part
        );
      }
    }
  map [ $_, [ split /(\d+)/ ] ], @unsorted;
  return @sorted;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:38:25+00:00Added an answer on May 29, 2026 at 5:38 am

    Your code does not work differently for version strings. Just add 9.02 9.2 to the list in this order. If you want 02 to come after 2, you have to inspect the case when $A == $B.

    Update: It means adding or length $A <=> length $B after the $A <=> $B.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.