A Simple Analysis of sorting by category

Source: Internet
Author: User

Sort by draggingAlgorithm

When we do some systems, we need users to sort products and categories.

In this case, we need to add an attribute named displayorder to the object.

To increase and decrease, you only need to change the two displayorders.

But if you drag it, It's not a simple exchange. Instead, you need to insert displayorder to the front.

For example, we have
Name displayorder

A 1
B 2
C 3
D 4
E 5
F 6
G 7

Before Pulling g to C, we must set displayorder of G to C.

Then, set C to D, and D to E, and E to F to G.
However, such efficiency is relatively poor. Especially in SQL, it may be troublesome to write this statement.

 

Because displayorder is at least one difference, we can implement it in a simpler way.

It is to add all displayorder + 1 after C and it, but this algorithm does not seem to be the best.
If it is a relatively long-distance drag, we have to change many items.

 

A better way is that the displayorder difference value is not 1. In this way, the displayorder of G can be set to C to some extent. C + 1 won't repeat D. To reduce updates.

 

 

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.