Suppose you now have 4 data to sort and are asked to come up with a solution

  • Implementation 1 has an initial weight of 0, changed one by one

    Give each element an initial weight of 0, and sort by setting the weight of each element each time

Advantages: simple implementation, can be any configuration weight to arrange any position

Disadvantages: Poor user experience, weight needs to be set for each element to sort, if the judgment of weight is not rigorous, it may lead to too much weight beyond the range of system storage

  • Realize 2 initial weight continuous, batch update weight

    Set a continuous weight range from small to large for each element, and perform two steps each time you need to change the order: 1. Set its own weight to the desired position weight 2. Reduce the weight of records less (greater) than the target weight by 1(plus 1)

Advantages: Simple implementation, good user experience, less operation steps for users to move elements

Disadvantages: The larger the weight span is, the more records need to be changed, which may cause performance problems

  • Implementation 3 uses linked list structure, modify pointing

Sorting is no longer reflected by the weight of the value of sorting, but the use of linked list idea, each element is stored in his previous and the next element address, each change of sorting is only need to change the corresponding element pointing

Advantages: Small changes, regardless of weight range values and performance issues caused by too many record changes

Disadvantages: Complex implementation, not database friendly storage, and can’t be fetched in a single Order Derby statement