我有一个ID列表,像这样: 5,3,2,4,1 ,然后我用这个ID从DB查询项目。但他们来自DB通常按id排序,我的意思是 1,2,3,4,5 。我需要将它们重新排列成与它们的id列表相同的顺序。我怎么能做到这一点?
编辑:澄清,因为许多人似乎对这个问题感到困惑。一个ID列表用于查询数据库,例如像这样:
SELECT * FROM Foo WHERE id in(5, 3,2,4,1)Foo对象的结果列表没有以相同的方式排序作为ID的列表。问题是如何获得与ID的初始列表相同顺序的Foo对象列表。
解决方案我想你必须在代码中重新排序查询结果:
public static void main(String [] args){ 列表<整数> ids = Arrays.asList(5,3,2,4,1,6); 列表< Foo>结果= Arrays.asList(新的Foo(1),新的Foo(8),新的Foo(2),新的Foo(4),新的Foo(5),新的Foo(7)); System.out.println(sortResults1:+ sortResults1(ids,results)); System.out.println(sortResults2:+ sortResults2(ids,results)); } private static List< Foo> sortResults1(List< Integer> ids,List< Foo>结果){ Foo [] sortedResultsArray = new Foo [ids.size()]; for(Foo result:results){ //查找此结果的id所需的位置 int pos = ids.indexOf(result.getId()); if(pos> = 0){ sortedResultsArray [pos] = result; } } 列表< Foo> sortedResults = new ArrayList<>(Arrays.asList(sortedResultsArray)); sortedResults.removeAll(Collections.singleton(null)); return sortedResults; } private static List< Foo> sortResults2(List< Integer> ids,List< Foo> results){ Collections.sort(results,ComparatorparingInt(item - > ids.indexOf(item.getId()))); 返回结果; $ / code>第一个解决方案省略了列表中没有的ID的ids。
比较结果的第二个解决方案将任何未知ID的结果放在结果列表的前面。
输出:
sortResults1:[Foo 5,Foo 2,Foo 4,Foo 1] sortResults2 [Foo 8,Foo 7,Foo 5,Foo 2,Foo 4,Foo 1]
注意:在这里找到类似的问题:排序具有特定顺序的(数组)列表 它具有TreeMap的合理答案。
I have list of ids, like this: 5, 3, 2, 4, 1 , then i query items from DB with that ids. But they coming from DB usually sorted by id, i mean 1, 2, 3, 4, 5. I need to rearrange them to be in the same order as they ids list. How can i achieve that?
EDIT: A clarification since many people seem confused about this question. A list of IDs is used to query the database, for example like this:
SELECT * FROM Foo WHERE id in (5, 3, 2, 4, 1)The resulting list of Foo objects is not ordered in the same way as the list of IDs. The question is how to obtain a list of Foo objects with the same order as the initial list of IDs.
解决方案I think you have to re-order the result of the query in code:
public static void main(String[] args) { List<Integer> ids = Arrays.asList(5, 3, 2, 4, 1, 6); List<Foo> results = Arrays.asList(new Foo(1), new Foo(8), new Foo(2), new Foo(4), new Foo(5), new Foo(7)); System.out.println("sortResults1: " + sortResults1(ids, results)); System.out.println("sortResults2: " + sortResults2(ids, results)); } private static List<Foo> sortResults1(List<Integer> ids, List<Foo> results) { Foo[] sortedResultsArray = new Foo[ids.size()]; for (Foo result : results) { // look up the required position of this result's id int pos = ids.indexOf(result.getId()); if (pos >= 0) { sortedResultsArray[pos] = result; } } List<Foo> sortedResults = new ArrayList<>(Arrays.asList(sortedResultsArray)); sortedResults.removeAll(Collections.singleton(null)); return sortedResults; } private static List<Foo> sortResults2(List<Integer> ids, List<Foo> results) { Collections.sort(results, ComparatorparingInt(item -> ids.indexOf(item.getId()))); return results; }The first solution omits any results with an ID that does not occur in the list of ids.
The second solution with the comparator puts any results with unknown IDs at the front of the result list.
Output:
sortResults1: [Foo 5, Foo 2, Foo 4, Foo 1] sortResults2[Foo 8, Foo 7, Foo 5, Foo 2, Foo 4, Foo 1]
Note: Found a similar question here: Sort an (Array)List with a specific order It has a reasonable answer with a TreeMap.
更多推荐
如何根据给定的模式重新排列列表中的项目?
发布评论