Sorting of DocumentList using one of the fields as a key

Hello

I have a documentList with following structure:

-Document
-field1
-field2.

I would like to Sort this document list using field1/field2 as the key. Would someone know the code for doing this.

Cheers

Sam.

Refer to the Java documents (<install_directory>\Developer\doc\API\Java):

com.wm.data.IDataUtil - sortIDataArrayByKey

Code Snippet:

***
	public static final void sortIDataDocsValues (IData pipeline)
        throws ServiceException
	{
		// --- <> ---
		// @subtype unknown
		// @sigtype java 3.5
		// [i] field:0:required keyField
		// [i] field:0:required datePattern {"dd/MM/yyyy kk:mm:ss.s"}
		// [i] field:0:required sortDescending {"true","false"}
		// [i] record:1:required values
		// [o] record:1:required sortedValues
		// [o] field:0:required sorted
		IData[] sortedItemList = null; 
		 
		// pipeline 
		IDataCursor pipelineCursor = pipeline.getCursor(); 
		IDataCursor pipelineCursor1 = pipeline.getCursor(); 
		IData[] itemList = IDataUtil.getIDataArray( pipelineCursor, "values" ); 
		
		int N = itemList.length; 
		IData[] itemListClone = new IData[N]; 
		try { 
		
			if (itemList != null) 
			{ 
				for (int i=0;i<itemList.length;i++) 
					if (itemList[i] != null) 
						itemListClone[i] = IDataUtil.deepClone(itemList[i]); 
			} 
			String keyField = IDataUtil.getString( pipelineCursor, "keyField" ); 
			boolean sortDescending = (Boolean.valueOf(IDataUtil.getString( pipelineCursor, "sortDescending" ))).booleanValue(); 
			pipelineCursor.destroy(); 
		
			if(itemList != null) 
			{ 
		   		String pattern = IDataUtil.getString( pipelineCursor, "datePattern" ); 
				if (pattern != null)
				{
					sortedItemList = IDataUtil.sortIDataArrayByKey(itemListClone,keyField, 
													IDataUtil.COMPARE_TYPE_TIME, pattern, sortDescending); 
				}
				else
				{
					sortedItemList = IDataUtil.sortIDataArrayByKey(itemListClone,keyField, 
													IDataUtil.COMPARE_TYPE_COLLATION, null,sortDescending); 
				}
			} 
		}catch (IOException e) {} 
		
		// pipeline 
		pipelineCursor = pipeline.getCursor(); 
		IDataUtil.put( pipelineCursor, "sorted", sortedItemList==null ? "false" : "true"); 
		IDataUtil.put( pipelineCursor,"sortedValues",itemListClone); 
		pipelineCursor.destroy(); 
		
		// --- <> ---



***

Thank you very much for the reply. I tried this code and it works only if I use the field at the highest level as the key. If use field1\field2(ie try to sort on a key at a lower level, it does not work.

I would appreciate any more suggestions or clues.

Cheers

Sam

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

Hi,