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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:49:56+00:00 2026-05-23T10:49:56+00:00

I have an array containing Roman numerals (as strings of course). Like this: $a

  • 0

I have an array containing Roman numerals (as strings of course). Like this:

 $a = array('XIX', 'LII', 'V', 'MCCXCIV', 'III', 'XIII');

I’d like to sort them according to the numeric values of these numerals, so the results should be something like:

 $sorted_a = array('III', 'V', 'XIII', 'XIX', 'LII', 'MCCXCIV');

So my question is: what is the best way to sort an array of Roman numerals? I know how to use the array sorting functions of PHP, I’m interested in the logic that goes on inside the comparison function.

EDIT: For simplicity, I’m only looking for a way that deals with strings constructed of the basic numerals in a standard way (no CCCC for example):

I, V, X, L, C, D, M

TEST RESULTS

I took the time to extensively test all the code examples that were posted. Two tests were taken, one with a random array of 20 Roman numerals, and a second with an array containing 4000 of those. Same machine, lot of iterations, an average time taken, and all this run several times. Of course this is nothing offical, just my own tests.

TEST WITH 20 NUMERALS:

  1. hakre, bazmegakapa – around 0.0005 s
  2. anemgyenge, Andrea, Dirk McQuickly – around 0.0010 s
  3. Joe Nelson – around 0.0050 s
  4. Rob Hruska – around 0.0100 s

TEST WITH 4000 NUMERALS:

  1. hakre, bazmegakapa – around 0.13 s
  2. anemgyenge – around 1.4 s
  3. Dirk McQuickly, Andrea – around 1.8 s
  4. Rob Hruska – around 2.8 s
  5. Joe Nelson – around 15 s (surprise, checked several more times)

I have a hard time awarding the bounty. hakre and I made the fastest versions, following the same route, but he made a variation of mine, which was previously based on borrible’s idea. So I will accept hakre’s solution, because that is the quickest and nicer than mine (IMO). But I will award the bounty to anemgyenge, because I love his version and a lot of effort seems to be put into it.

  • 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-23T10:49:57+00:00Added an answer on May 23, 2026 at 10:49 am

    Picking your class to convert roman numbers to integers, a user-defined sort callback can handle this to sort the array:

    $a = array('XIX', 'LII', 'V', 'MCCXCIV', 'III', 'XIII');
    
    $bool = usort($a, function($a, $b) {
        return RomanNumber::Roman2Int($a) - RomanNumber::Roman2Int($b);
    });    
    var_dump($a);
    

    So here you find the logic inside the comparison function: if both values are of the same weight, return 0. If the first is lower than the second, return < 0 (e.g. -1), otherwise the second is larger than the first so return > 0 (e.g. 1).

    Naturally any other type of function that returns the decimal value for a roman number would work as well.

    Edit:

    As you commented, you do not want to run the conversion for each pair. That’s fine, with a help of an additional array which contains all converted values, you can run the sort on the decimal values and use that sorting on the roman numbers as well (Demo):

    $a = array('XIX', 'LII', 'V', 'MCCXCIV', 'III', 'XIII');
    $b = array_map('RomanNumber::Roman2Int', $a);
    array_multisort($b, $a);
    var_dump($a);
    

    array_multisort PHP Manual does most of the magic here.

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

Sidebar

Related Questions

I have an array containing strings of this format: [0] => title|url|score|user|date [1] =>
So lets say I have an array containing a hash like this: [{head=> {title=>$20,000
I have an array containing a list of files. I want to sort it
I have a 1d array containing Nd data, I would like to effectively traverse
I have an array containing strings with prices and sometimes surrounded by characters. How
I have an array containing an unknown number of items that I would like
I have an array that contain some fields like this ctl00_ctl00_cphBody_bodycph_content_rdo_SID_25_SortOrder_17 ctl00_ctl00_cphBody_bodycph_content_rdo_SID_25_SortOrder_18 ctl00_ctl00_cphBody_bodycph_content_rdo_SID_25_SortOrder_19 I
I'm killing myself and dehydrating trying to get this array to sort. I have
I have an array containing much more items than just this one. This is
I have a array containing data. This array contains only image data/ or it

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.