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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:36:45+00:00 2026-05-23T17:36:45+00:00

Yes, the title is confusing, but I will keep this simple. Imagine a photo

  • 0

Yes, the title is confusing, but I will keep this simple. Imagine a photo page and on it are comments. These comments can be nested by replying to them. I need to find the correct sort order for them.

I am storing these hierarchies using a “lineage”. It is a textual, flattened value of the position of a single comment in the hierarchy. Here is a correctly sorted thread. I’m only showing the lineage column:

903
909
909-1404
1405

903 is the first comment, 909 the second. 1404 is a reply to the second comment, and finally, 1405 is the last comment, a non-reply. This is how they should be sorted. I’m struggling to reach that result though. This is what I get if I simply do this:

ORDER BY lineage

1405
903
909
909-1404

The error here is that 1405 should be at the bottom. I know of the (+0) trick to order text like it is a number:

ORDER BY (lineage+0)

Which gives me this:

903
909-1404
909
1405

This time, the threaded comment (1404) is wrongly sorted, it should come after the parent (909).

Is there an easy way to accomplish my desired sorting? The only thing I could think of is to fill comment ids with zeroes in front, but that would be an incredibly painful migration process that I desperately want to avoid.

  • 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-23T17:36:45+00:00Added an answer on May 23, 2026 at 5:36 pm

    Pad the numbers in lineage with zeros so you can properly order them as strings.

    Of course for proper padding you need to know the max number you want to store there.

    There are other ways to store hierarchic tree relations (parent links, nested tree sets). Your approach has some benefits for specific tasks, but, as you can see, also has its limits.

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

Sidebar

Related Questions

Yes title is a little confusing but let me tell you what I have
The title sounds confusing.. but im sure this is possible! I have one button,
Yes, I know the title sounds a little suspicious. I will try to explain
Yes, title sounds a bit confusing, so I'll explain what I mean: suppose you
I've been attempting to debug this all morning but can't seem to find the
Yes, the title doesn't make much sense, but here's my situation. I have two
Yes as the title says: How can i detect an file extension? $_POST['fname']; is
...Yes I've seen: Best Resources for Learning JavaFX? but it doesn't really answer the
Yes, There's More Than One Way To Do It but there must be a
Yes, I know you could use regular objects as associative arrays in JavaScript, but

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.