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

  • Home
  • SEARCH
  • 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 157037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:25:57+00:00 2026-05-11T10:25:57+00:00

I’d like to store an additional column in a table as a ‘sort value’,

  • 0

I’d like to store an additional column in a table as a ‘sort value’, which is a numeric representation of the title column, such that the order of such values represents the string’s natural alphabetical sort order. Ie, so that I can retrieve rows ordered by the sort value, and they’ll be in natural sort order – and when I insert a new row, I can generate the numeric value and know that value relative to others will represent the string’s position in an alphabetic search, accurate to the first X letters or so.

A couple of reasons for this: firstly, I would like a more natural ordering than a plain ordering offered by a DB server, where things like ‘The’ and ‘A’ and punctuation are ignored at the start, and numbers are treated ‘naturally’.

Secondly, this is for an index with a lot of permutations – it will save space, and perhaps time when traversing an index with many rows.

What I am after for is the algorithm to translate the string to that numeric value, or just, I suppose, a normalised string value.

I am using PHP and MySQL.

I’m afraid that ‘pull everything from the DB and sort in PHP using natcasesort()’ is not a solution for this particular situation, as I’d like to retrieve rows (using order by and group by) in sorted order before they get to a join or limit clause. Thanks.

Edit:

Thanks for answers so far. It’s just occurred to me that the fact my application uses UTF-8 is quite relevant. With that said, I think the practicality of representing the initial part of a string in a packed/numeric form is a stretch, maybe just some sort of normalised form (everything case-folded, numbers zero-padded, and as many characters as possible normalised to their root ie ã to a) would be appropriate.

  • 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. 2026-05-11T10:25:58+00:00Added an answer on May 11, 2026 at 10:25 am

    Thanks for the answers so far. I just wanted to update people with the solution I’m going with. I’ve taken an approach that is different from that which I envisaged in my question.

    To recap, I wanted to store a representations of strings such that when retrieved in binary order, whatever I stored for ‘8 Mile’ would be sorted before whatever I stored for ‘101 Dalmations’.

    For each number in the string, which is essentially a sequence of digits, I insert a digit before them that describes how many digits the number is.

    So, ‘8’ becomes ’18’, and ‘101’ becomes ‘3101’. It adds some redundancy to the number, in that you are using more digits than you need and some values won’t exist, but they now have the property that a binary sort will sort the numbers into numerical order. ‘101’ would have sorted before ‘8’ beforehand, which was undesired. After adding that extra digit, ’18’ sorts before ‘3101’.

    Note: if the number is 9 or more digits long, I add two digits to the start: the number of digits in the number minus 9, then a 9, then the number. This allows for numbers up to 18 digits: good enough for me.

    I’m also normalising the string in other ways too – everything to lower case, Unicode characters will be translated into the closest ascii equivalent, and ‘a’, ‘an’, and ‘the’ will be stripped if they are the first word.

    I gave up on making the string into one big numeric value; it is still a string, it’s just that it’s not designed for humans to read.

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

Sidebar

Ask A Question

Stats

  • Questions 110k
  • Answers 110k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If all you want is to create multiple instances of… May 11, 2026 at 9:34 pm
  • Editorial Team
    Editorial Team added an answer Not sure I complete understand what you want but would… May 11, 2026 at 9:34 pm
  • Editorial Team
    Editorial Team added an answer You could change a a/b/c.mp3 path into a - b… May 11, 2026 at 9:34 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.