gpt4 book ai didi

java - 交换/重新排序记录

转载 作者:行者123 更新时间:2023-12-01 15:28:31 29 4
gpt4 key购买 nike

我在数据库中有带有排序顺序号的记录。现在我想创建一个带有向上和向下按钮的用户界面来重新排序它们。制作两个 functionsUp(record)functionDown(record) 的最佳算法是什么,以便通过更改 orderNum 来重新排列它们,并将该数字保留到数据库中。

这是我想要实现的示例(在 foo=24 之前,在 foo=25 之后):

在按下之前

id | name | orderNum
--------------------
1 | foo | 24
2 | bar | 25
3 | doe | 26

之后

id | name | orderNum
--------------------
1 | foo | 25
2 | bar | 24
3 | doe | 26

最佳答案

假设您正在使用 Java Collections,您可以编写一个实现 Comparable 的类并使用实用程序 Collections.sort()。有关订购系列的更多信息 here .

关于java - 交换/重新排序记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9875241/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com