重新排序列表帮助或算法需要 [英] reordering list help or algo needed

查看:72
本文介绍了重新排序列表帮助或算法需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我假设我在db表中有一个任务列表(表格如下:

ID,任务,排序)


我想要重新排序任务列表而不更新表中

中的所有记录。我不想对每条记录运行一个sql update stmt

更新sort字段。


还有其他简单方法吗?那里必须有一个简单的算法




非常感谢任何帮助。

So lets assume I have a list of tasks in db table (table looks like:
ID, task, sort)

And I want to reorder the task list without updating all the records in
the table. I dont want to run a sql update stmt against every record to
update the "sort" field.

Any other easy way to do this? there has got to be a simple algo out
there.

any help is greatly appreciated.

推荐答案

按排序字段的加入顺序返回''任务''

SELECT任务FROM db.table ORDER BY sort


按排序字段的降序重新调整任务

SELECT任务FROM db.table ORDER BY sort DESC

Return ''task'' in accending order of ''sort'' field
SELECT task FROM db.table ORDER BY sort

Retun ''task'' in decending order of ''sort'' field
SELECT task FROM db.table ORDER BY sort DESC


是的我知道ORDER BY。这不是问题。


当使用UI显示时,我希望用户能够通过向上或向下移动任务来重新排序

列表基于优先级。


然后我需要将这些更改保存到数据库中,为了做到这一点,我需要重新更新所有记录。这显然不是每次有变化时都可以非常可扩展/高效地执行此操作假设

任务列表很长。


任何其他想法?

Yes i know about ORDER BY. That is not the issue.

When displayed using a UI, i want the user to be able to reorder the
list by moving a task up or down based on priority.

I then need to save those changes to the database, and in order to do
this i would need to reupdate all the records. This is obviously not
very scalable/efficient to do this everytime there is a change assuming
the task list is long.

any other ideas?


tg **** @ gmail。 com 写道:
tg****@gmail.com wrote:
是的我知道ORDER BY。这不是问题。

使用UI显示时,我希望用户能够根据优先级上下移动任务来重新排序
列表。

然后我需要将这些更改保存到数据库中,为了做到这一点,我需要重新更新所有记录。假设任务列表很长,每次有变化时,这显然不具备可扩展性/效率。

任何其他想法?
Yes i know about ORDER BY. That is not the issue.

When displayed using a UI, i want the user to be able to reorder the
list by moving a task up or down based on priority.

I then need to save those changes to the database, and in order to do
this i would need to reupdate all the records. This is obviously not
very scalable/efficient to do this everytime there is a change assuming
the task list is long.

any other ideas?



多少件物品?如果十几个,甚至一百个,整个表格的更新

是亚秒级。当然,如果你遇到任何麻烦,我会否认曾经提出这个建议。


表中有多少列?另一种策略是直接交换除排序之外的所有列的

值。在一次交易中:


开始交易

更新优先级设置id = ValFromRow15,task = ValfromRow15其中sort = 3

update优先级设置id = ValFromRow3,task = ValfromRow3其中sort = 15

commit


你可能遇到ID问题,嗯。无论如何,该栏目在做什么?


看来你也可能希望客户只强制某些类型的动作

所以你没有当你试图提交时,很容易弄清楚。也许

允许用户在

到服务器之前一次调整一行的优先级,所以最多只能交换两行。 br />

-

Kenneth Downs

安全数据软件公司

(Ken)nneth @ (Sec)ure(Dat)a(.com)



How many items? If a dozen, even a hundred, an update to the entire table
is sub-second. Of course I will deny ever having made the suggestion if
you run into any trouble.

How many columns in the table? Another strategy is to directly swap the
values of all columns except "sort" in a single transaction:

begin transaction
update priorities set id=ValFromRow15,task=ValfromRow15 where sort=3
update priorities set id=ValFromRow3,task=ValfromRow3 where sort=15
commit

You may have trouble with ID, hmmm. What is that column doing anyway?

It also seems you may want the client to force only certain types of moves
so you don''t have a big jumble to sort out when you try to commit. Perhaps
allow the user to adjust the priority of only one row at a time before a
trip to the server, so at most you are only ever swapping two rows.

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)


这篇关于重新排序列表帮助或算法需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆