Here is a code that we have used for sorting a document list based on a key field :
Inputs : itemList, keyField, fields, sortDescending, type
Output: itemList, sorted
This service takes as input an ‘itemList’ to be sorted according to the ‘keyField’.
‘fields’ are the fields in the list.
If ‘sortDescending’ is true then it sorts it in descending order else in ascending order.
‘type’ is whether we have to sort on Numeric or String field. (Time can be added in a manner similar to String - see API)
synchronized(class name)
{
try
{
IData[] sortedItemList = null;
// pipeline
IDataCursor pipelineCursor = pipeline.getCursor();
IData[] itemList = IDataUtil.getIDataArray( pipelineCursor, "itemList" );
String keyField = IDataUtil.getString( pipelineCursor, "keyField" );
String [] fields = IDataUtil.getStringArray( pipelineCursor, "fields");
String type = IDataUtil.getString( pipelineCursor, "type");
boolean sortDescending = (Boolean.valueOf(IDataUtil.getString( pipelineCursor, "sortDescending" ))).booleanValue();
pipelineCursor.destroy();
String tmpPipelineValue = "";
if(itemList != null)
{
if(type.equals("String"))
{
sortedItemList = IDataUtil.sortIDataArrayByKey(itemList, keyField, IDataUtil.COMPARE_TYPE_COLLATION, null, sortDescending);
}
else if(type.equals("Numeric"))
{
for(int i=0; i<(itemList.length); i++)
{
IDataCursor tmpCursor = itemList[i].getCursor();
tmpPipelineValue = IDataUtil.getString(tmpCursor,keyField);
double num1;
Double d = Double.valueOf(tmpPipelineValue);
num1 = d.doubleValue();
for(int j= i+1; j<(itemList.length); j++)
{
IDataCursor tmpCursor1 = itemList[j].getCursor();
tmpPipelineValue = IDataUtil.getString(tmpCursor1, keyField);
double num2;
d = Double.valueOf(tmpPipelineValue);
num2 = d.doubleValue();
if(sortDescending)
{
if(num2 > num1)
{
IData tmpObj = IDataFactory.create();
IDataUtil.copy( itemList[i], tmpObj);
for(int fd=0; fd < fields.length; fd++)
{
IDataUtil.remove(tmpCursor, fields[fd]);
}
IDataUtil.copy( itemList[j], itemList[i]);
tmpCursor = itemList[i].getCursor();
tmpPipelineValue = IDataUtil.getString(tmpCursor, keyField);
d = Double.valueOf(tmpPipelineValue);
num1 = d.doubleValue();
for(int fd=0; fd < fields.length; fd++)
{
IDataUtil.remove(tmpCursor1, fields[fd]);
}
IDataUtil.copy( tmpObj, itemList[j]);
}
}
else
{
if(num2 < num1)
{
IData tmpObj = IDataFactory.create();
IDataUtil.copy( itemList[i], tmpObj);
for(int fd=0; fd < fields.length; fd++)
{
IDataUtil.remove(tmpCursor, fields[fd]);
}
IDataUtil.copy( itemList[j], itemList[i]);
tmpCursor = itemList[i].getCursor();
tmpPipelineValue = IDataUtil.getString(tmpCursor, keyField);
d = Double.valueOf(tmpPipelineValue);
num1 = d.doubleValue();
for(int fd=0; fd < fields.length; fd++)
{
IDataUtil.remove(tmpCursor1, fields[fd]);
}
IDataUtil.copy( tmpObj, itemList[j]);
}
}
}
}
}
}
// pipeline
pipelineCursor = pipeline.getCursor();
if(type.equals("String"))
{
IDataUtil.put( pipelineCursor, "sorted", sortedItemList==null ? "FALSE" : "TRUE");
}
else if(type.equals("Numeric"))
{
IDataUtil.put( pipelineCursor, "sorted", "TRUE");
}
//IDataUtil.put( pipelineCursor, "itemList", itemList);
pipelineCursor.destroy();
}
catch(Exception ex)
{
throw(new ServiceException(ex.toString()));
}
}
HTH,
Anant
#webMethods#Integration-Server-and-ESB