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 4256408
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:20:58+00:00 2026-05-21T05:20:58+00:00

I recently wrote a simple utility in Perl to count words in a file

  • 0

I recently wrote a simple utility in Perl to count words in a file to determinate its frequency, that’s how many times it appears.

It’s all fine, but I’d like to sort the result to make it easier to read. An output example would be:

4:an
2:but
5:does
10:end
2:etc
2:for

As you can see, it’s ordered by word, not frequency. But with a little help of :sort I could reorganize that. Using n, numbers like 10 go to the right place (even though it start with 1), plus a little ! and the order gets reversed, so the word that appears more is the first one.

:sort! n
10:end
5:does
4:an
2:for
2:etc
2:but

The problem is: when the number is repeated it gets sorted by word — which is nice — but remember, the order was reversed!

for -> etc -> but

How can I fix that? Will I have to use some Vim scripting to iterate over each line checking whether it starts with the previous number, and marking relevant lines to sort them after the number changes?

  • 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-21T05:20:59+00:00Added an answer on May 21, 2026 at 5:20 am

    From the vim :help sort:

    The details about sorting depend on the library function used.  There is no
    guarantee that sorting is "stable" or obeys the current locale.  You will have
    to try it out.
    

    As a result, you might want to perform the sorting in your Perl script instead; it wouldn’t be hard to extend your Perl sort to be stable, see perldoc sort for entirely too many details.

    If you just want this problem finished, then you can replace your :sort command with this:

    !sort -rn --stable (it might be easiest to use Shift-V to visually select the lines first, or use a range for the sort, or something similar, but if you’re writing vim scripts, none of this will be news to you. 🙂

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

Sidebar

Related Questions

I recently wrote a DLL in C# (.Net 2.0) which contains a class that
I recently wrote some javascript code that filled a drop down list based on
I have a Python script I recently wrote that I call using the command
I recently wrote a parser in Python using Ply (it's a python reimplementation of
I recently wrote a class for an assignment in which I had to store
I recently wrote a piece of code which did SomeClass someObject; mysqlpp::StoreQueryResult result =
I recently wrote mailing list software in Ruby On Rails. I would like to
I recently wrote a small tool to generate a class for each tier I
Recently I wrote a function to generate certain sequences with nontrivial constraints. The problem
Recently an application I wrote started not working on Internet Explorer. There has been

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.