java实现标签排序置顶
设计思路
置顶:将该数据放到全局数据收尾,其余数据顺序不变【将需要置顶数据的sort设置为全局Min(sort)-1】
置尾:将数据放到全局末尾,其余不变【将需要置尾的数据sort设置为全局Max(sort)+1】
交换:两位位置交换,其余不变【将需要交换数据的sort互换即可】
拖动:局部顺序变化【将4插到1后面,降顺序变动的数据4、2、3的sort取出然后对sort进行从小到大排序,依次赋值4、3、2】
DB设计
tag表,主键为tag_id,排序字段为sort,其中sort设计为有符号类型会存在负值,但能很好保证最小变动原则,高效完成排序操作。新增或指定操作理论上sort不会重复,但可能因为删除而不连续,拖动可以将数据排序后一次提交
实现
import java.util.*; public class TagService { private List<Tag> tags; public TagService(List<Tag> tags) { this.tags = tags; } public void updateTagSort(Tag tag, String action) { int currentIndex = tags.indexOf(tag); int minSort = Collections.min(tags, Comparator.comparingInt(Tag::getSort)).getSort(); int maxSort = Collections.max(tags, Comparator.comparingInt(Tag::getSort)).getSort(); if (action.equals("up")) { if (currentIndex > 0) { swapTags(currentIndex, currentIndex - 1); } } else if (action.equals("down")) { if (currentIndex < tags.size() - 1) { swapTags(currentIndex, currentIndex + 1); } } else if (action.equals("top")) { moveToTop(tag, minSort); } else if (action.equals("tail")) { moveToTail(tag, maxSort); } else if (action.equals("move")) { // 这里需要额外的逻辑来确定移动到哪里,可能需要额外的参数 // 例如:move(tag, targetIndex) } else { throw new IllegalArgumentException("Unsupported action: " + action); } // 假设这里会更新数据库或持久化层 // updateDatabase(); } private void swapTags(int index1, int index2) { Collections.swap(tags, index1, index2); Tag tempTag = tags.get(index1); int tempSort = tempTag.getSort(); tempTag.setSort(tags.get(index2).getSort()); tags.get(index2).setSort(tempSort); } private void moveToTop(Tag tag, int minSort) { int currentIndex = tags.indexOf(tag); int newSort = minSort - 1; tag.setSort(newSort); Collections.rotate(tags.subList(0, currentIndex + 1), -currentIndex); // 更新后续标签的sort值以保持连续性 for (int i = 0; i < tags.size(); i++) { if (tags.get(i).getSort() >= newSort) { tags.get(i).setSort(tags.get(i).getSort() + 1); } } } private void moveToTail(Tag tag, int maxSort) { int currentIndex = tags.indexOf(tag); int newSort = maxSort + 1; tag.setSort(newSort); // 将当前标签移到列表末尾 Collections.rotate(tags.subList(currentIndex, tags.size()), tags.size() - currentIndex); // 更新前面标签的sort值以保持连续性(如果需要) for (int i = 0; i < currentIndex; i++) { if (tags.get(i).getSort() > newSort) { tags.get(i).setSort(tags.get(i).getSort() - 1); } } } // 其他方法,如获取所有标签、根据sort排序等 // ... public static void main(String[] args) { // 示例数据 List<Tag> tags = new ArrayList<>(); tags.add(new Tag(1, 1)); tags.add(new Tag(2, 2)); tags.add(new Tag(3, 3)); tags.add(new Tag(4, 4)); TagService tagService = new TagService(tags); // 假设我们要将tagId为3的标签置顶 Tag tagToTop = tags.stream().filter(tag -> tag.getTagId() == 3).findFirst().orElse(null); if (tagToTop != null) { tagService.updateTagSort(tagToTop, "top"); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端