java 如何重新排序对象列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11086913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to reorder a list of objects?
提问by
Firstly, this is going to sound like homework, but it ain't. Just a problem I'm trying to solve at work.
首先,这听起来像家庭作业,但它不是。只是我在工作中试图解决的一个问题。
I have a list of objects, the objects have a sequence number which indicates their order in the list in the UI. Example:
我有一个对象列表,这些对象有一个序列号,用于指示它们在 UI 列表中的顺序。例子:
public class Task {
Long id;
String name;
Long seq;
}
The table in my UI has "up" and "down" links on each row of the table for moving the tasks up and down in the list.
我的 UI 中的表格在表格的每一行上都有“向上”和“向下”链接,用于在列表中上下移动任务。
I'm implementing two methods to handle the reordering. These methods are call by ajax in the web UI.
我正在实施两种方法来处理重新排序。这些方法由 Web UI 中的 ajax 调用。
public void incTaskSeq(List<Task> allTasks, Task taskToMove)
For example; if I have t1.seq=1, t2.seq=2, t3.seq=3, t4.seq=4, t5.seq=5 and I want to increment the place of t3, then t3.seq becomes 4, and t4.seq must become 3.
例如; 如果我有 t1.seq=1, t2.seq=2, t3.seq=3, t4.seq=4, t5.seq=5 并且我想增加 t3 的位置,那么 t3.seq 变为 4,并且t4.seq 必须变为 3。
public void decTaskSeq(List<Task> allTasks, Task taskToMove)
Similarly; if I have t1.seq=1, t2.seq=2, t4.seq=3, t3.seq=4, t5.seq=5 and I want to decrement the place of t4, then t4.seq becomes 2, and t2.seq must become 3. Resulting in:
相似地; 如果我有 t1.seq=1, t2.seq=2, t4.seq=3, t3.seq=4, t5.seq=5 并且我想减少 t4 的位置,那么 t4.seq 变为 2,并且t2.seq 必须变为 3。导致:
t1.seq=1, t4.seq=2, t2.seq=3, t3.seq=4, t5.seq=5
t1.seq=1, t4.seq=2, t2.seq=3, t3.seq=4, t5.seq=5
I'm a little stuck on the best way to do this.
我对执行此操作的最佳方法有些困惑。
I was thinking of putting all the tasks in a HashMap and then sort the map by the sequence number. Then locate the taskToMove in the map, change the sequence number, and then change all the affected tasks sequences.
我正在考虑将所有任务放在一个 HashMap 中,然后按序列号对地图进行排序。然后在地图中定位到taskToMove,更改序号,然后更改所有受影响的任务序列。
But this approach seems inelegant. Does anyone have any ideas how I should do this?
但这种方法似乎不雅。有没有人有任何想法我应该怎么做?
Thanks, Rob
谢谢,罗布
采纳答案by Edwin Buck
Use a Comparator
, which is the Java interface for sorting non-naturally.
使用 a Comparator
,这是用于非自然排序的 Java 接口。
public TaskSequenceComparator implements Comparator<Task> {
public int compare(Task one, Task two) {
return one.getSequence() - two.getSequence();
}
}
...
List<Task> tasks = ...;
Collections.sort(tasks, new TaskSquenceComaprator());
// tasks is now sorted by sequence.
You can create multiple Comparator
classes to implement each kind of supported sort. Then you can select from them when you need the list sorted in a particular manner.
您可以创建多个Comparator
类来实现每种支持的排序。然后,当您需要以特定方式对列表进行排序时,您可以从它们中进行选择。
回答by SJuan76
Implement a Comparator
for each criteria you want to sort for.
Comparator
为您要排序的每个条件实现一个。
Create a new ordered Collection
object (maybe a TreeSet) passing it the Comparator needed. Do .addAll of the objects.
创建一个新的有序Collection
对象(可能是 TreeSet),将它传递给所需的 Comparator。做 .addAll 的对象。
回答by smp7d
Use a doubly linked list and swap the sequence numbers when you move an item.
使用双向链表并在移动项目时交换序列号。
回答by jvataman
Just let the List keep tarck of ordering. (Except You always need to update the seqence number).
只需让列表保持排序。(除非您总是需要更新序列号)。
public void incTaskSeq(List<Task> allTasks, Task taskToMove){
int movTaskNum = 0;
for(int i=0;i<allTasks.size();i++){
if(allTasks.get(i).equals(taskToMove))
movTaskNum = i;
}
allTasks.remove(taskToMove);
allTasks.add(i-1, taskToMove);
}
sorry for typos
抱歉打字错误