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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:08:12+00:00 2026-06-11T05:08:12+00:00

I wish to sort an array of strings so that the strings wind up

  • 0

I wish to sort an array of strings so that the strings wind up in the following order:

@set = ('oneM', 'twoM', 'threeM', 'sixM', 'oneY', 'twoY', 'oldest');

As you may notice, these represent time periods so oneM is the first month, etc. My problem is that I want to sort by the time period, but with the strings as they are I can’t just use ‘sort’, so I created this hash to express how the strings should be ordered:

my %comparison = (
    oneM    => 1,
    twoM    => 2,
    threeM  => 3,
    sixM    => 6,
    oneY    => 12,
    twoY    => 24,
    oldest  => 25,
);

This I was hoping would make my life easier where I can do something such as:

foreach my $s (@set) {
    foreach my $k (%comparison) {
        if ($s eq $k) {
            something something something

I’m getting the feeling that this is a long winded way of doing things and I wasn’t actually sure how I would actually sort it once I’ve found the equivalent… I think I’m missing my own plot a bit so any help would be appreciated

As requested the expected output would be like how it is shown in @set above. I should have mentioned that the values in @set will be part of that set, but not necessarily all of them and not in the same order.

  • 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-06-11T05:08:13+00:00Added an answer on June 11, 2026 at 5:08 am

    You’ve choose good strategy in precomputing data to form easy to sort. You can calculate this data right inside sorting itself, but then you’d be wasting time for recalculation each time sort needs to compare value, which happens more than once through process. On the other hand, the drawback of cache is, obviously, that you’d need additional memory to store it and it might slow down your sort under low memory condition, despite doing less calculations overall.

    With your current set up sorting is as easy as:

    my @sorted = sort { $comparison{$a} <=> $comaprison{$b} } @set;
    

    While if you want to save memory at expense of CPU it’d be:

    my @sorted = sort { calculate_integer_based_on_input{$a} <=> calculate_integer_based_on_input{$b} } @set;
    

    with separate calculate_integer_based_on_input function that would convert oneY and the like to 12 or other corresponding value on the fly or just inline conversion of input to something suitable for sorting.

    You might also want to check out common idioms for sorting with caching computations, like Schwartzian transform and Guttman Rosler Transform.

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

Sidebar

Related Questions

I have an array that I wish to sort, it contains images with file
I have an array of strings and I wish to find out if that
I wish to sort a space separated table , with the numerical value that
I wish to sort 3 separate tables that are logically related to each other
I have a set of unique strings, and I wish to show them with
I have a the need for key value pair that I wish to sort
I have an array of strings such as blue, green, red and I wish
I am attempting to implement a selection sort of an array in NASM that
I have a list of object I wish to sort in C#.Net 3.5, the
Essentially I have an NSMutableArray which contains several NSDictionaries . I wish to sort

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.