On the table of tree-type structure and the problem of sorting

Source: Internet
Author: User
Sort | tree Structure | problem
Just now a colleague came across a sort of tree-structured table, which made me think of a very deep hidden bug. I usually design a tree-structured table like this: There are three columns in the table related to the sort, Id,parentid, Treekey

ID: Unique primary key for this record
ParentID: Number of the parent record
Treekey: Represents a hierarchy, as in the form of xx.xxx.xxxx

The key is Treekey, I've previously asked for the number of the parent ID, like this

ID ParentID Treekey
---------------------------------------------------------------------
1 NULL 1
2 1 1.2
3 1 1.3
...
10 1 1.10
11 1 1.11

In this case, it's a problem to sort by treekey, because Treekey is a string, and 1.10 goes to 1.2, 1.3, obviously, that's not the result we want. To solve the fact is also very simple, in front of the number 0, for example, 1.10 written 0001.0010,1.2 written on the 0001.0002, so that the sorting will not have a problem.

This reminds me of a very early look at Microsoft's a tree-related software code, see they are 000x.000x this way to deal with, I still feel how they are so troublesome, now want to, just find out that they are ignorant, hehe.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.