0

When i am passing a list of ids to the jdo query the results comes as ordered list. Here is the sample code.

ArrayList<Long> ids=new ArrayList<Long>();
        ids.add(3720L);
        ids.add(3707L);
        ids.add(3712L);
        ids.add(3726L);
        ids.add(3710L);
        PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(p);
        PersistenceManager pm = pmf.getPersistenceManager();
//      Extent<?> e = pm.getExtent(User.class, true);
        Query query = pm.newQuery(User.class);
        String filterString = null;
        filterString = "ids.contains(this.id) && this.enabled==true";
        query.setFilter(filterString);
        query.declareParameters("java.util.Collection ids");
        @SuppressWarnings("unchecked")
        List<User> allUsers = (List<User>) query.execute(ids);

The above code always gives output as in asc order of id. How can i get the result in the same order.

user3309305
  • 181
  • 1
  • 7
  • No idea what you mean. Define your ORDER BY perhaps ... but then I've no idea what order you mean –  May 30 '18 at 08:06
  • 1
    HI @BillyFrost . If you see the above example my input is in the order 3720,3707,3712,3726,3710. By running the above example results comes as 3707,3710,3712,3720,3726 which is in ASC order. How can i get the output as same order as input – user3309305 May 30 '18 at 08:26
  • add `ORDER BY ids.get(this.id)` perhaps? but then since you are returning a max of 5 objects, makes more sense to order them yourself in Java, maybe using a comparator ... –  May 30 '18 at 08:36
  • I used this **query.setOrdering("ids.get(this.id)")** in the code its throwing exception `Cannot perform operation "get" on org.datanucleus.store.rdbms.sql.expression.CollectionLiteral`. Here i shown a sample case in real time it will be in 100s in number. – user3309305 May 30 '18 at 08:50
  • Actually my suggestion of using the get method of a List is wrong ... you need the List position which is obtained using `indexOf`. Doubt that is supported. Why dont you post WHAT you think the SQL should be to order by what you want ... then there is basis for comment –  May 31 '18 at 15:47
  • Your list "ids" is only used to get matching records. I guess you get the allUsers sorted by id because it is the default in your setup/datastore. You could build a new list by looping over your original ids and get the User from allUsers. See https://stackoverflow.com/questions/36210577/how-can-i-sort-a-list-in-java-by-another-list-of-ids – Oli Mar 25 '22 at 10:08

0 Answers0