How to write a java flow to sort a recordlist ?

Question: Hi,

The webmethods 3.6 doesn't provide a buid-in service to sort a recordlist. I try to write a java flow to sort recordlist but I failed. I can write a java flow to sort a stringlist and it works ( codes quoted below ) . Does anyone enlighten me where to get this information or provide some sample codes ? Thanks in advance.

Best regards,
C.H. Peng
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
// pipeline
IDataHashCursor pipelineCursor = pipeline.getHashCursor();
pipelineCursor.first( "StringList" );
String[] StringList = (String[])pipelineCursor.getValue();
pipelineCursor.destroy();

Arrays.sort(StringList);

Answer:
If you manage i would love to have the code - my java is not good enough and i failed ....

jon

Answer:
I think the reason the above code doesn't work is that the record list is an array of objects - you will probalbly only be sorting the object IDs not the key you wanted

jon

Answer:
Found the code at a webmethods group website ( http://eai.ittoolbox.com/groups/groups.asp?v=webmethods-l). Shared the code below.

regards,
C.H. Peng
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
IDataHashCursor pipelineCursor = pipeline.getHashCursor();
try
{
pipelineCursor.first("recordList");
IData recordList[] = (IData[]) pipelineCursor.getValue();
pipelineCursor.first("sortOrder");
String sortOrder = (String) pipelineCursor.getValue();
pipelineCursor.first("sortOnKey");
String sortOnKey = (String) pipelineCursor.getValue();
IData sortedRecordList[] = null;
if (sortOrder == null)
{
sortOrder = "A";
}
if (recordList == null)
{
throw new ServiceException("invalid input - recordList - may be empty");
}
if (sortOnKey == null)
{
throw new ServiceException("invalid input - sortOnKey - may be empty");
}
int sLength, count, tcount;
String temp, stemp;
sLength = recordList.length;
IData tempRecord = null;
IDataHashCursor tempCursor = null;
String tempKeyValue = null;
IData currentRecord = null;
IDataHashCursor currentCursor = null;
String currentKeyValue = null;
IData previousRecord = null;
IDataHashCursor previousCursor = null;
String previousKeyValue = null;
IDataHashCursor fromCursor = null;
IDataHashCursor toCursor = null;
for (count = 1; count < sLength; count++)
{
if (sortOrder.equals("A"))
{
currentRecord = IDataFactory.create();
fromCursor = recordList[count].getHashCursor();
toCursor = currentRecord.getHashCursor();
while(fromCursor.next())
{
if (toCursor.first((String) fromCursor.getKey()))
{
toCursor.setValue((String) fromCursor.getValue());
}
else
{
toCursor.insertAfter((String) fromCursor.getKey(), (String) fromCursor.getValue());
}
}
currentCursor = currentRecord.getHashCursor();
currentCursor.first(sortOnKey);
currentKeyValue = (String) currentCursor.getValue();
previousRecord = IDataFactory.create();
fromCursor = recordList[count-1].getHashCursor();
toCursor = previousRecord.getHashCursor();
while(fromCursor.next())
{
if (toCursor.first((String) fromCursor.getKey()))
{
toCursor.setValue((String) fromCursor.getValue());
}
else
{
toCursor.insertAfter((String) fromCursor.getKey(), (String) fromCursor.getValue());
}
}
previousCursor = previousRecord.getHashCursor();
previousCursor.first(sortOnKey);

previousKeyValue = (String) previousCursor.getValue();
if (currentKeyValue.compareTo(previousKeyValue) < 0)
{
tempRecord = IDataFactory.create();
fromCursor = currentRecord.getHashCursor();
toCursor = tempRecord.getHashCursor();
while(fromCursor.next())
{
if (toCursor.first((String) fromCursor.getKey()))
{
toCursor.setValue((String) fromCursor.getValue());
}
else
{
toCursor.insertAfter((String) fromCursor.getKey(), (String) fromCursor.getValue());
}
}
tcount = count-1;
while ((tcount>=0)&&(previousKeyValue.compareTo(currentKeyValue) > 0))
{
fromCursor = recordList[tcount].getHashCursor();
toCursor = recordList[tcount+1].getHashCursor();
while(fromCursor.next())
{
if (toCursor.first((String) fromCursor.getKey()))
{
toCursor.setValue((String) fromCursor.getValue());
}
else
{
toCursor.insertAfter((String) fromCursor.getKey(), (String) fromCursor.getValue());
}
}
tcount--;
if (tcount >= 0)
{
previousRecord = recordList[tcount];
previousCursor = previousRecord.getHashCursor();
previousCursor.first(sortOnKey);
previousKeyValue = (String) previousCursor.getValue();
}
}
fromCursor = tempRecord.getHashCursor();
toCursor = recordList[tcount+1].getHashCursor();
while(fromCursor.next())
{
if (toCursor.first((String) fromCursor.getKey()))
{
toCursor.setValue((String) fromCursor.getValue());
}
else
{
toCursor.insertAfter((String) fromCursor.getKey(), (String) fromCursor.getValue());
}
}
}
}
else
{
currentRecord = recordList[count];
currentCursor = currentRecord.getHashCursor();
currentCursor.first(sortOnKey);
currentKeyValue = (String) currentCursor.getValue();

previousRecord = recordList[count-1];
previousCursor = previousRecord.getHashCursor();
previousCursor.first(sortOnKey);
previousKeyValue = (String) previousCursor.getValue();
if (currentKeyValue.compareTo(previousKeyValue) > 0)
{
tempRecord = currentRecord;
tcount = count-1;
while ((tcount>=0)&&(previousKeyValue.compareTo(currentKeyValue) < 0))
{
fromCursor = recordList[tcount].getHashCursor();
toCursor = recordList[tcount+1].getHashCursor();
while(fromCursor.next())
{
if (toCursor.first((String) fromCursor.getKey()))
{
toCursor.setValue((String) fromCursor.getValue());
}
else
{
toCursor.insertAfter((String) fromCursor.getKey(), (String) fromCursor.getValue());
}
}
tcount--;
if (tcount >= 0)
{
previousRecord = recordList[tcount];
previousCursor = previousRecord.getHashCursor();
previousCursor.first(sortOnKey);
previousKeyValue = (String) previousCursor.getValue();
}
}
fromCursor = tempRecord.getHashCursor();
toCursor = recordList[tcount+1].getHashCursor();
while(fromCursor.next())
{
if (toCursor.first((String) fromCursor.getKey()))
{
toCursor.setValue((String) fromCursor.getValue());
}
else
{
toCursor.insertAfter((String) fromCursor.getKey(), (String) fromCursor.getValue());
}
}
}
}
}
if (pipelineCursor.first("recorList"))
{
pipelineCursor.setValue(recordList);
}
else
{
pipelineCursor.last();
}
pipelineCursor.insertAfter("sortedRecordList", recordList);
}
catch(Exception e)
{
if (e instanceof ServiceException)
{
throw (ServiceException)e;
}
else
{
throw (ServiceException)e;
}
}
finally
{
pipelineCursor.destroy();
}
Copyright ?2007 - 2008 www.jt77.com