Foros de discusión

How to use OrderByComparator

Ed F., modificado hace 15 años.

How to use OrderByComparator

Expert Mensajes: 280 Fecha de incorporación: 27/06/06 Mensajes recientes
Anyone have any ideas what I'm doing wrong with my OrderByComparator implementation?

For example I want to sort some results of a ResourcesFolderUtil function by an attribute of ResourcesFolder, say numEntries

V-- this function
    public static java.util.List<com.ext.portlet.resources.model.resourcesfolder> findByNumEntries(
        long parentFolderId, int start, int end,
        com.liferay.portal.kernel.util.OrderByComparator obc)
        throws com.liferay.portal.SystemException {
        return getPersistence().findByNumEntries(parentFolderId, start, end, obc);
    }
</com.ext.portlet.resources.model.resourcesfolder>


So I write my OrderByComparator:

public class OrderByNumEntries extends OrderByComparator{
	
	public int compare(Object o1, Object o2){
		ResourcesFolder e1 = (ResourcesFolder)o1;
		ResourcesFolder e2 = (ResourcesFolder)o2;

                System.out.println("!!! I'm comparing stuff !!!");	

		if (e1.getNumEntries() &gt; e2.getNumEntries()){
			return 1;
		}
		if (e1.getNumEntries() &lt; e2.getNumEntries()){
			return -1;
		}
		return 0;
	}
	
}

I know, I use system.out.println, old school right.

Anyway, in ResourcesFolderLocalServiceImpl:
public List getSubFoldersByNumEntries(long folderId, int start, int end) throws SystemException {
		return ResourcesFolderUtil.findByNumEntries(folderId, start, end, new OrderByNumEntries());
	}


And then I call that, and it's not in order...at all.

Number of Entries returned goes 5, 7, 9, 2, 0, 8, 5, 2, etc. etc.
And, that println "I'm doing stuff!" never shows up in the log emoticon

Any idea what I did wrong?

All help greatly appreciated.
-Ed
thumbnail
Thiago Moreira, modificado hace 15 años.

RE: How to use OrderByComparator

Liferay Legend Mensajes: 1449 Fecha de incorporación: 10/10/07 Mensajes recientes
Humm, to me sounds you are never calling the right method...

Another tip, don't write your code inside the *Util classes, these classes are always re generated by the sevice builder. Instead write your code at *ServiceImpl classes.

For futher information take a look on this wiki entry.

Regards
Ed F., modificado hace 15 años.

RE: How to use OrderByComparator

Expert Mensajes: 280 Fecha de incorporación: 27/06/06 Mensajes recientes
Yeah, that was a typo. I do write it in the impl classes.
thumbnail
Thiago Moreira, modificado hace 15 años.

RE: How to use OrderByComparator

Liferay Legend Mensajes: 1449 Fecha de incorporación: 10/10/07 Mensajes recientes
humm, ok!

Try to debug from the method call of the your client code until the creation of the OrderByComparator...

Good luck!
thumbnail
Adrian Rodriguez Monedero, modificado hace 10 años.

RE: How to use OrderByComparator

New Member Mensajes: 15 Fecha de incorporación: 17/10/10 Mensajes recientes
This thread does not have a solution, and the question is still valid in 2014, so, I am bringing it up.

The problem that the original poster points out is that his println line does not show up in the standard output. Why is this? When is the compare method of the comparator used?

Thanks!
thumbnail
Prakash Khanchandani, modificado hace 9 años.

RE: How to use OrderByComparator

Expert Mensajes: 329 Fecha de incorporación: 10/02/11 Mensajes recientes
Is the compare method ever used? even if we implement the different methods like getOrderBy and getOrderByFields of the OrderByComparator, still it seems it does not use the compare method. It just uses the other methods to fetch sorted results.

I think the compare method is present only so that we can use it on an already retrieved list and liferay as such does not use it internally.

I may be wrong and would really like to be corrected.
thumbnail
Igor Beslic, modificado hace 8 años.

RE: How to use OrderByComparator

New Member Mensajes: 17 Fecha de incorporación: 17/08/11 Mensajes recientes
Probably this is late answer, but let me answer it for others.
Ed. F. please take a look at
com.liferay.portal.util.OrderByComparatorFactoryImpl

orderByComparators are usually created via factory class to prevent wrong interface implementations (and OrderByNumEntries is wrong). In my opinion factory create method is enough for what you need.

Anyway, if your business case requires some special compare (see how default compare works) you are free to go with your own implementation but please follow pattern of private inner class implementation:
com.liferay.portal.util.OrderByComparatorFactoryImpl.DefaultOrderByComparator

Comparing your and default implementation you will notice you miss table parametrization, and column-orderingDirection pairs. Based on your description you should define constructor to enable you this:
new OrderByNumEntries("table_name", "numEntries", true)
or
new OrderByNumEntries("table_name", "numEntries", false)
for asc order.

Thank you! (and please close the case if answer is good)
Kevin Neibarger, modificado hace 4 años.

RE: How to use OrderByComparator

Regular Member Mensajes: 105 Fecha de incorporación: 2/02/18 Mensajes recientes
I'm sure this is closed or an old post but I noticed an issue with OrderByComparator as implemented as Liferay. What happens in the case of a list we where we want data with null values to be pushed to the bottom of the list? For this solution I had to implement my own comparator and determine if the compared values were null and handle them appropriately. I woud be nice if Liferay's API would handle this instead, but like a lot of my other work arounds have demonstrated, it does not.