Questions about sorting

Source: Internet
Author: User
Now there is a classification table that contains the following fields

    • Id
    • ParentID Parent ID
    • Orders the order in which the item is displayed at its level, the smaller the value, the lower the previous
    • Text content

For example, such as the following data (text field content is only to facilitate the description of the writing, the actual content is not so):

ID ParentID Order text
1 0 10 1
2 0 0 2
3 1 20 1-2
4 1 10 1-1
5 3 20 1-2-2
6 3 10 1-2-1
7 2 30 2-1

How to sort it (roughly sorted by ParentID and order)

ID ParentID Order text
2 0 0 2
7 2 30 2-1
1 0 10 1
4 1 10 1-1
3 1 20 1-2
6 3 10 1-2-1
5 3 20 1-2-2

In simple terms, it is an infinite classification of sub-categorical sorting problems. Sort by parent-child category first, and then sort all sibling categories by order.

I am using the most stupid method, is to separate each layer of classification read out (recursive), sorted into the total number of groups, and currently only tested on the 3-level classification of support, but feel this efficiency is low ah, do not know what good algorithm, hope God help, or pointing twos, do a good job based on PHP.

Reply content:

Now there is a classification table that contains the following fields

    • Id
    • ParentID Parent ID
    • Orders the order in which the item is displayed at its level, the smaller the value, the lower the previous
    • Text content

For example, such as the following data (text field content is only to facilitate the description of the writing, the actual content is not so):

ID ParentID Order text
1 0 10 1
2 0 0 2
3 1 20 1-2
4 1 10 1-1
5 3 20 1-2-2
6 3 10 1-2-1
7 2 30 2-1

How to sort it (roughly sorted by ParentID and order)

ID ParentID Order text
2 0 0 2
7 2 30 2-1
1 0 10 1
4 1 10 1-1
3 1 20 1-2
6 3 10 1-2-1
5 3 20 1-2-2

In simple terms, it is an infinite classification of sub-categorical sorting problems. Sort by parent-child category first, and then sort all sibling categories by order.

I am using the most stupid method, is to separate each layer of classification read out (recursive), sorted into the total number of groups, and currently only tested on the 3-level classification of support, but feel this efficiency is low ah, do not know what good algorithm, hope God help, or pointing twos, do a good job based on PHP.

Data structure: Sets a virtual parent category for all top-level classifications, and then each cluster has a reference to its ordered subcategory array.
Sort operation: Performs an operation on each node that gets all the subcategories and then saves them to an array.

If you have to put it in the same array, then put the results in the same order after the sorting is done.
Of course, I recommend modifying the method of reading an array so that it can parse the tree structure.

I have a train of thought. When retrieving data from a database, order by parentid ASC, order ASC, according to ParentID and order

Get all the data in a loop into PHP:
foreach ($list as $v) {
$tmp [$v [' parentid ']][] = $v;
}
This is a well-ordered, root node with a subset
One more recursive, add the root node to the line.

Query, all queries out, only to order sort

Iterate through the constructed array at once

$data = array();foreach ($rows as $r) {    $data['item'][$r['id']] = $r;    $data['parent'][$r['id']] = $r['pid'];    $data['children'][$r['pid']][] = $r['id'];}

Use the general traversal data['children'][当前需要遍历的父ID] , and then by data['item'][当前ID] finding the specified entry

Not to meet the squirt

  • 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.