一个关于排序的问题,求高性能解决办法。

SONY 2012-04-06 02:23:03
需求:
index name
1 规则1
2 规则2
3 规则3
4 规则4
5 规则5
6 规则6
7 规则7
8 规则8
9 规则9
10 规则10
11 规则11
… ……
现在要实现自定义排序,
例如:
1:让“规则9”排到“规则2”下面
2:让“规则3”排到“规则7”下面
3:删除“规则5”
要求以上操作要保持index的连续性。

我个人的解决办法:
1:查出3-8的规则,遍历把index+1
“规则9”的index,update=3;
2:查出>7的规则,遍历把index+1
“规则3”的index,update=8;
然后查出>3的规则,遍历把index-1
3.删除“规则5”,查出>5的规则,遍历把index-1


这里的规则信息比较庞大,这样的效率太差,但是实在没想出什么好的办法。请各位说说有没有更好的解决方案。


这里的index字段是备用字段,我准备设置为“index”使用,如上,
大家最好能不改变数据库实现,入需改变,能提高性能,也接受为好方法。



...全文
196 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
司机 2012-04-20
  • 打赏
  • 举报
回复
如果是内存中操作 排用LinkedHashMap() 有序又有键值,可方便替换
qybao 2012-04-10
  • 打赏
  • 举报
回复
在8L的时候本来就想提出追加排序字段来处理,如果LZ只是为了显示,那么排序字段还是有意义的,但如果不光是为了显示,其实排序字段意义也不大了,因为不管怎么排序,都需要重新更新index。而频繁地更新数据库会降低性能,所以提出让LZ用前台的排序来处理,即使后面需要用到排序后的结果处理,也可以把排序好的entity传給下一个处理方法就好了。
安特矮油 2012-04-10
  • 打赏
  • 举报
回复
如果你的规则就一种或者几种的情况下还是可以考虑扩展几列用作排序,通过使用的规则不同而使用不同的列进行排序,相当于是穷举法。
SONY 2012-04-10
  • 打赏
  • 举报
回复
我这优先级,可能实时就要用的,再前台操作,也不怎么合适。行吧,看来也没什么好办法了,只能按照老办法开发了。谢谢你们了。
SONY 2012-04-09
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]
除非本来的需求就是直接更新数据库[/Quote]

不只是显示,因为后面还有其他一个实现需要,调用数据库,进行先后排序。

你敲了那么多代码,还是很感谢你呀。
qybao 2012-04-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
引用 4 楼 的回复:
1 首先规则3和规则9交换
update table set name = 规则9 where index = 3
update table set name = 规则3 where index = 9


1:让“规则9”排到“规则2”下面,不能打乱原有的顺序呀。
交换后,原有的顺序会乱了。
[/Quote]

哦,原来每条规则是独立的,我还以为结合起来一起看了
如果是独立的规则,可以追加一些用于排序的字段来处理,不过这样频繁的update数据库,性能会降低的,除非本来的需求就是直接更新数据库,否则,如果只是为了显示,没必要每条规则都去更新数据库,可以考虑在前台用Collections.sort(Comparator c)的方式来处理,比如

List<Your_Entity> list = getYourEntityFromDB(); //从数据库选取数据
//规则1排序
Collections.sort(list, new Comparator<Your_Entity>() {
public int compare(Your_Entity e1, Your_Entity e2) {
if (e1.getIndex() == 1) {
return -1;
} else if (e2.getIndex() == 1) {
return 1;
} else if (e1.getIndex() == 9) {
return -1;
} else if (e2.getIndex() == 9) {
return 1;
}
return e1.getIndex() - e2.getIndex();
}
});
//显示
for (int i=0; i<list.size(); i++) {
System.out.printf("index=%d, name=%s\n", i+1, list.get(i).getName());
}
//规则2排序
Collections.sort(list, new Comparator<Your_Entity>() {
public int compare(Your_Entity e1, Your_Entity e2) {
if (e1.getIndex() == 3) {
return (e2.getIndex() <= 7 ? 1 : -1);
} else if (e2.getIndex() == 3) {
return (e1.getIndex() <= 7 ? -1 : 1);
}
return e1.getIndex() - e2.getIndex();
}
});
//显示
for (int i=0; i<list.size(); i++) {
System.out.printf("index=%d, name=%s\n", i+1, list.get(i).getName());
}
//规则3
Collections.sort(list, new Comparator<Your_Entity>() {
public int compare(Your_Entity e1, Your_Entity e2) { //缺省排序(按index升序排序)
return e1.getIndex() - e2.getIndex();
}
});
list.remove(4); //删除index=5的数据,这里考虑是按顺序排列的数据都存在,否则要自己查找再删除
//显示
for (int i=0; i<list.size(); i++) {
System.out.printf("index=%d, name=%s\n", i+1, list.get(i).getName());
}

qybao 2012-04-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
引用 4 楼 的回复:
1 首先规则3和规则9交换
update table set name = 规则9 where index = 3
update table set name = 规则3 where index = 9


1:让“规则9”排到“规则2”下面,不能打乱原有的顺序呀。
交换后,原有的顺序会乱了。
[/Quote]

哦,原来每条规则是独立的,我还以为结合起来一起看了
如果是对立的规则,可以追加一些用于排序的字段来处理,不过这样频繁的update数据库,性能会降低的,除非本来的需求就是直接更新数据库,否则,如果只是为了显示,没必要每条规则都去更新数据库,可以考虑在前台用Collections.sort(Comparator c)的方式来处理,比如

List<Your_Entity> list = getYourEntityFromDB(); //从数据库选取数据
//规则1排序
Collections.sort(list, new Comparator<Your_Entity>() {
public int compare(Your_Entity e1, Your_Entity e2) {
if (e1.getIndex() == 1) {
return -1;
} else if (e2.getIndex() == 1) {
return 1;
} else if (e1.getIndex() == 9) {
return -1;
} else if (e2.getIndex() == 9) {
return 1;
}
return e1.getIndex() - e2.getIndex();
}
});
//显示
for (int i=0; i<list.size(); i++) {
System.out.printf("index=%d, name=%s\n", i+1, list.get(i).getName());
}
//规则2排序
Collections.sort(list, new Comparator<Your_Entity>() {
public int compare(Your_Entity e1, Your_Entity e2) {
if (e1.getIndex() == 3) {
return (e2.getIndex() <= 7 ? 1 : -1);
} else if (e2.getIndex() == 3) {
return (e1.getIndex() <= 7 ? -1 : 1);
}
return e1.getIndex() - e2.getIndex();
}
});
//显示
for (int i=0; i<list.size(); i++) {
System.out.printf("index=%d, name=%s\n", i+1, list.get(i).getName());
}
//规则3
Collections.sort(list, new Comparator<Your_Entity>() {
public int compare(Your_Entity e1, Your_Entity e2) { //缺省排序(按index升序排序)
return e1.getIndex() - e2.getIndex();
}
});
list.remove(4); //删除index=5的数据,这里考虑是按顺序排列的数据都存在,否则要自己查找再删除
//显示
for (int i=0; i<list.size(); i++) {
System.out.printf("index=%d, name=%s\n", i+1, list.get(i).getName());
}

SONY 2012-04-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
1 首先规则3和规则9交换
update table set name = 规则9 where index = 3
update table set name = 规则3 where index = 9
[/Quote]

1:让“规则9”排到“规则2”下面,不能打乱原有的顺序呀。
交换后,原有的顺序会乱了。


SONY 2012-04-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
1 首先规则3和规则9交换
update table set name = 规则9 where index = 3
update table set name = 规则3 where index = 9
[/Quote]

这个是替换,不行呀。


1:让“规则9”排到“规则2”下面,“规则9”后面仍然是"规则3", 交换会打乱原有的顺序。


-AJ- 2012-04-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

链表---
[/Quote]

是数据库方面的啊,我看错了。
SONY 2012-04-09
  • 打赏
  • 举报
回复
对,是优先级问题,一个扩展列就够了,因为后面的就是要用排序,移动后的顺序进行优先级。
看来,没什么好的办法了,只能用我开始的那种思路了。谢谢各位了。
安特矮油 2012-04-09
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]
引用 9 楼 的回复:
除非本来的需求就是直接更新数据库

不只是显示,因为后面还有其他一个实现需要,调用数据库,进行先后排序。

你敲了那么多代码,还是很感谢你呀。
[/Quote]
多半你所说的其他的排序需要用到这里的排序也就是个优先级的问题,你给数据库扩展几个列出来,每个列代表一个种方式,按照你的需要把所有的规则排序,在不同规则的时候你就order by 不同的列,这样效率会比你去操作数据库的数据来的快的多。当然这种方式只适用于你的规则排列是有限的情况下,如果太多的话维护也很麻烦的。

如果规则多了那你也只有动态去更新数据库里面的数据了。只能说这种需求真的太变态了。
qybao 2012-04-06
  • 打赏
  • 举报
回复
1 首先规则3和规则9交换
update table set name = 规则9 where index = 3
update table set name = 规则3 where index = 9

2 然后规则3和规则8交换
update table set name = 规则3 where index = 8
update table set name = 规则8 where index = 9

1和2可以合并为
update table set name = 规则9 where index = 3
update table set name = 规则3 where index = 8
update table set name = 规则8 where index = 9

3 删除规则5
delete table where index = 5

4 更新规则6以后的index为index-1
update table set index=index-1 where index > 5



-AJ- 2012-04-06
  • 打赏
  • 举报
回复
链表---
Ade子夜 2012-04-06
  • 打赏
  • 举报
回复
期待好的方法
安特矮油 2012-04-06
  • 打赏
  • 举报
回复
你这样完全是通过数据库对ID的排序来排序了,不过除了这种办法,也没有其他更好的了,你的都是自定义的,完全没有规律可寻,灵活本来就是牺牲性能来完成的。如果你能定下一些规则,而且规则不多的情况下,你可以增加几个用于排序的字段,这样效率会高很多

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧