2

I have a table that stores hierarchical relationships.They stored as strings like 1.1, 1.1.1, 1.1.2, 1.2 ... Thus,when I need to sort them in hierarchical order I need to invoke my own sort function.Aren't I?But I think it is too long write own effective alghoritm (n*ln(n)).I want to use little function that just compares two values and use it with ORDER BY statement.Can I do this?Are there another ways to achive this goals?Thanks.

3
  • Can you pad the individual values out to a fixed length with zeroes, e.g. 0001.0001.0002? Commented Sep 6, 2012 at 13:09
  • 1
    If each level is the same length then a text sort comes out in the right order. Commented Sep 6, 2012 at 13:14
  • yes,it's a pretty solution.Please make it as answer. Commented Sep 6, 2012 at 13:16

2 Answers 2

2

Left padding each level with zeroes to a fixed length will allow a straightforward text sort. With your sample values:

0001.0001
0001.0001.0001
0001.0001.0002
0001.0002
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

order by
    parsename(col,4)*1,
    parsename(col,3)*1,
    parsename(col,2)*1,
    parsename(col,1)*1

1 Comment

parsname function used for small strings but I can have very 'deep' strings - in advance unknown how 'deep' the hierarchy will be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.