简体   繁体   English

MySQL中冒泡排序的替代方法

[英]Alternative to bubble sort in MySQL


the problem lies most likely in fact, I am not asking google(or stackoverflow) the right question, but since this simple thing is driving me nuts. 问题实际上很可能出在这里,我不是在问google(或stackoverflow)正确的问题,但是由于这个简单的事情使我发疯。 Sorry for long post, but want to make situation clear. 很抱歉发布了很长时间,但是想弄清楚情况。

I have a mysql table of records ID, person_name, priority 我有一个记录ID,person_name,优先级的mysql表

ID and person_name are quite selfexplanatory, the priority is used to sort the records descending order - highest = top priority. ID和person_name的含义不言而喻,优先级用于对记录降序排序-最高=最高优先级。

I have following records, for simplicity, IDs correspond the sorting order: 我有以下记录,为简单起见,ID对应于排序顺序:

1, "John A", 3
2, "John B", 2
3, "John C", 1
4, "John D", 0

This worked fine until I imported a record from a export from other machine, suddenly there are two records with same priority. 在我从其他计算机的导出中导入记录之前,这一直很好,突然之间有两个优先级相同的记录。

1, "John A", 3
2, "John B", 2
3, "John C", 1
5, "John E", 1
4, "John D", 0

When the records are now ordered by priority DESC, name ASC, the ID3 and ID5 are sorted alphabetically which does not correspond the situation. 现在按优先级DESC,名称ASC,ID3和ID5对记录进行排序时,将按字母顺序排序,这与情况不符。 If user wants to shift priority of ID5 to be HIGHER than ID2, I can't just increment the priority, since it would match ID2 and ID5 would appear BELOW ID2 due to alpha-order. 如果用户希望将ID5的优先级更改为高于ID2,我不能仅仅增加优先级,因为它会匹配ID2,并且由于字母顺序,ID5会显示在ID2以下。

Question: Is there a way to do this more EFFECTIVELY than this, or just completely different approach: 问题:是否有比这更有效的方法,或者是完全不同的方法:

  1. NEAREST_PRIORITY = Determine priority of nearest record with higher priority NEAREST_PRIORITY =确定具有更高优先级的最近记录的优先级
  2. Increase priority of all records with priority > NEAREST_PRIORITY by 1 将优先级> NEAREST_PRIORITY的所有记录的优先级提高1
  3. Set current record priority = NEAREST_PRIORITY+1 设置当前记录优先级= NEAREST_PRIORITY + 1

This will actually not work quite well if there are several records with NEAREST_PRIORITY... 如果有多个带有NEAREST_PRIORITY的记录,这实际上将不能很好地工作...

thanks for reading long post and any ideas. 感谢您阅读冗长的帖子和任何想法。

Interesting problem, it depends how you resolve the priority clashes of imported records. 有趣的问题,这取决于您如何解决导入记录的优先级冲突。 Let's assume that, if there's a clash, then imported records get priority. 我们假设,如果发生冲突,则导入的记录将具有优先权。 Before import multiply all priorities by 2, then import the new records but multiply their priorities by 2 and add 1 during the import. 导入之前,将所有优先级乘以2,然后导入新记录,但将其优先级乘以2,并在导入期间加1。 Now you'll have: 现在您将拥有:

1, "John A", 6
2, "John B", 4
5, "John E", 3
3, "John C", 2
4, "John D", 0

If you need your priorities to be continuous, or you do a lot of imports, then you'll have to do some work to reset the numbers but at least the order is correct. 如果您需要保持优先级连续,或者需要执行很多导入操作,则必须做一些工作来重置数字,但是至少顺序是正确的。

You can use the same solution for a priority change where there's a clash, multiply by two then set the new priority to be new priority + 1. So to move ID4 to before ID5 it's new priority would be (3 * 2) + 1 and you'd have: 您可以对发生冲突的优先级更改使用相同的解决方案,将其乘以2,然后将新优先级设置为新优先级+1。因此,将ID4移至ID5之前,其新优先级将为(3 * 2)+ 1和您将拥有:

1, "John A", 12
2, "John B", 8
4, "John D", 7
5, "John E", 6
3, "John C", 4

Again, things will escalate rapidly if you do this so you might want to reset the numbers. 同样,如果您这样做,事情将会迅速升级,因此您可能想要重置数字。 This answer shows an example of sequential numbering that you could adapt. 答案显示了您可以调整的顺序编号示例。

I was thinking about linked lists and implementing a priority table, something like 我在考虑链接列表并实现优先级表,例如

{ id, userId, comesAfterUserId, comesBeforeUserId }

Then any priority move should only change two entries. 然后,任何优先级移动都只能更改两个条目。 But I can't work out how to sort that in SQL directly, so you'd have to process in your app. 但是我不知道如何直接在SQL中对它进行排序,因此您必须在应用程序中进行处理。 I have a feeling that's a more elegant solution but the multiple by two should work too. 我觉得这是一个更优雅的解决方案,但乘以2的倍数也应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM