cancel
Showing results for 
Search instead for 
Did you mean: 

Sorting IResourceList problem

Former Member
0 Kudos

Hello Everyone,

I have a problem with the sort method of the IResourceList class, i wrote the following code:


public static void sortResouceListByMenuDescription(IResourceList resourceList) throws Exception
{
     instanceDescriptionProperty();
     ResourcePropertyComparator rRPC = new ResourcePropertyComparator(pDescripcion , true);
     resourceList.sort(rRPC); 
}

That code sort a KM folder content, once it is sorted i printed it and got this list:


Cobranza Pura y Express
Envío de Enteros vía SIAC
Operación Central del Sistema de Pago electrónico de Uso Ampliado -SPEUA-   <--- Third
Operación Central de Otros Pasivos                                          <--- Fourth
Operación Central PEMEX
Recepción de Fondos de Instituciones vía SIAC

The third and fourth strings are wrong the four one should be first because "de" has a lower value than "del".

Can anyone help me to get the right sort?

Thank you in advanced.

Regards.

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Here is the code i wrote:


import java.util.Comparator;
import com.sapportals.wcm.repository.IProperty;
import com.sapportals.wcm.repository.IPropertyName;
import com.sapportals.wcm.repository.IResource;
import com.sapportals.wcm.repository.PropertyName;

public class KMResourceComparator implements Comparator
{
	private final String _nameSpace = "http://mydomain.com/xmlns/custom";
	private final String _propertyName = "menu_description";
	private boolean _ascending;
	
	public KMResourceComparator(boolean ascending)
	{
		_ascending = ascending;		
	}
	public int compare(Object arg0, Object arg1)
	{
		try
		{
			int invertSort = _ascending ? 1 : -1;
			String description1 = getMenuDescription((IResource)arg0);
			String description2 = getMenuDescription((IResource)arg1);			

			return description1.compareTo(description2) * invertSort;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return 0;
		}
	}
	private String getMenuDescription(IResource resource) throws Exception
	{
		IPropertyName descriptionProperty = new PropertyName(_nameSpace, _propertyName);
		IProperty property = resource.getProperty(descriptionProperty);
		
		return property.getStringValue();
	}	
}

Thank you

Best regards.

carsten_buechert
Contributor
0 Kudos

Hi Roberto,

it seems like the used comparator (ResourcePropertyComparator) does not what it should do

You may write your own comparator to solve your issue.

If you need any more help, please do not hesitate to come back with further questions.

Greetings,

Carsten