Friday, March 25, 2011

Sorting a string list

My goodness.  You would think this would be easy.  Searched WmPublic and PSUtilities and found nothing.  Googled and didn't find much either.  So I created a java service sortStringList of my own.

Inputs:
inList - StringList
ascending - boolean

Outputs:
sortedList - StringList



    public static final void sortStringList(IData pipeline) throws ServiceException {
  
      
        // pipeline
        IDataCursor pipelineCursor = pipeline.getCursor();
            String[]    inList = IDataUtil.getStringArray( pipelineCursor, "inList" );
            String    ascending = IDataUtil.getString( pipelineCursor, "ascending" );
        pipelineCursor.destroy();
              
        // pipeline
        IDataCursor pipelineCursor_1 = pipeline.getCursor();
        String[]    sortedList = inList.clone();
        if (ascending.equalsIgnoreCase("true")) {
            Arrays.sort(sortedList);
        } else {
            List<String> list = Arrays.asList(sortedList);
          
            Collections.sort(list,
Collections.reverseOrder() );
          
        }
      
        IDataUtil.put( pipelineCursor_1, "sortedList", sortedList );
        pipelineCursor_1.destroy();
          
    }

6 comments:

  1. IS8 and PsUtilities have sort services for document lists. You can convert your string list (just by mapping)into a document list and sort it.

    ReplyDelete
    Replies
    1. how to convert a document list into a string list

      Delete
  2. Hi Kelvin,

    This would be more straight forward right:-

    http://wiki.customware.net/repository/display/WMFAQ/How+do+I+sort+a+Document+List

    Regards,
    Pua

    ReplyDelete
  3. Hi Kevin,

    Just another elegant approach for this.
    Instead of:

    Collections.sort(list);
    Collections.reverse(list);

    try

    Collections.sort(list, new Comparator() {
    @Override
    public int compare(String arg0, String arg1) {
    return arg1.compareTo(arg0);
    }
    });

    ReplyDelete
  4. ... or even better:

    Collections.sort( list, Collections.reverseOrder() );

    ReplyDelete
  5. Great Job - thanks!

    ReplyDelete