Bug 40504

Summary: Provide sort task like UNIX sort function
Product: Ant Reporter: David Leal <achepati67>
Component: Core tasksAssignee: Ant Notifications List <notifications>
Status: RESOLVED FIXED    
Severity: enhancement Keywords: PatchAvailable
Priority: P2    
Version: 1.6.5   
Target Milestone: 1.8.0   
Hardware: Other   
OS: other   
Attachments: SortFilter class, testing class and testing framework

Description David Leal 2006-09-13 22:56:47 UTC
Just to suggest to add the sort function like the UNIX sort function which is 
very powerfull.

Thanks in advance,

David
Comment 1 Antoine Levy-Lambert 2006-09-14 01:28:10 UTC
Hello David,

I would recommend to implement this not as a task but as a filter.

see http://ant.apache.org/manual/CoreTypes/filterchain.html

If you have some time you could try to write yourself the filter java
implementation and attach it to this bug report.

Regards,

Antoine
Comment 2 Matt Benson 2006-09-14 14:02:57 UTC
of interest, with Ant 1.7 you could use <string> resources in a <sort>
collection and sort strings, then push the sorted list to a file with
<pathconvert> and <echo>.  The only thing missing is an easy way to create
<string> resources to begin with.  Example:

<project>
  <pathconvert pathsep="${line.separator}">
    <sort>
      <resources>
        <string value="foo" />
        <string value="bar" />
        <string value="baz" />
        <string value="etc" />
      </resources>
    </sort>
  </pathconvert>
</project>

Yields:

Buildfile: build.xml
[pathconvert] bar
[pathconvert] baz
[pathconvert] etc
[pathconvert] foo

BUILD SUCCESSFUL
Total time: 0 seconds


:)
Comment 3 Matt Benson 2006-09-28 14:08:25 UTC
Added a <tokens> resource collection to SVN HEAD.  It's pretty cute IMHO.
Comment 4 David Leal 2006-09-30 10:41:51 UTC
Antoine,

Thanks for your suggestion, I am going to try implement it, but...if you take 
a look into the UNIX sort syntax there are a lot of options:

http://www.xmission.com/~comphope/unix/usort.htm

Do you have any suggestion about how to attack the all possible input 
options?, they are not only simple parameters, for example you can specify the 
priority of each field or column position will be take firt on the comparation 
criteria, for example:

sort -o $2 -k1.1,1.6 -k1.49,1.57 -k1.714,1.717 -k1.712,1.713 -k1.710,1.711 -
k1.694,1.694 -k1.718,1.729 $1

will sort taking first the position starting at column 1 to 6, then the string 
starting at column 49 to 57. All such fields are specified from the first 
column (k1*), but you can specify them via relative position of a given field 
(a field can be identified by tabs or spaces for example. As you can see such 
sort function is very powerfull.

Please could you give me any hint about that. I am a Ant user, but I have 
never implemented any feature on Java.

Thanks,

David
Comment 5 Jeffrey E. Care 2006-10-01 08:08:58 UTC
It's unrealistic to expect Ant to provide completely identical function to a
dedicated command line utility.

If you need all of the function of sort, I would suggest that you exec it.
Comment 6 Matt Benson 2006-10-01 08:26:45 UTC
Additionally you can write all the custom ResourceComparators you like and use 
them with a <sort> collection of <tokens> in Ant 1.7 .  All it takes is a 
little imagination!
Comment 7 David Leal 2006-10-01 14:24:12 UTC
Created attachment 18948 [details]
SortFilter class, testing class and testing framework

I have included a patch for adding a new filter SortFilter class. I have
considered the following features on it:
- Sort lines of the file in ascendant order.
- Sort lines in descendant order.
- Sort file using a Comparator class for user configurated sorting criterium.
Comment 8 David Leal 2006-10-01 14:47:19 UTC
Dear Antoine and others,

Following your recomendation I have added a patch with an implementation of 
SortFilter, it implements ChainableReader, because I think it have to, but I 
am not sure.

Because the UNIX sort function, has a lot of different possibilities, such 
amount of possible options from Java point of view can be only considered 
alowing the user just to define a Comparator implementation. That is my 
approach in general for this features, except for common criterias such us: 
Reverse order, considered via reverse parameter.

This approach allow in a future multiple extension points adding new 
parameters.

Note: I didn't implement the test of invoking recursively two or more sorting 
criteria at the same time (That is why I guess it has to implement 
ChainableReader). I guess it is easy to implement this test, invoking ant like 
this:

<target name="testSortFilterTwo" depends="init">
    <copy file="input/sort.sortDefault.test"
      tofile="result/sort.sortDefault.test">
      <filterchain>
        <filterreader classname="org.apache.tools.ant.filters.SortFilter">
          <param name="reverse" value="true"/>
        </filterreader>
        <filterreader classname="org.apache.tools.ant.filters.SortFilter">
        </filterreader>
      </filterchain>
    </copy>
</target>

the result has to be the same as testSortFilterNoArgs test case.

On the patch I have respected the same directory structure as in the SVN:

http://svn.apache.org/repos/asf/ant/core/trunk/src/

Regards,

David

P.S.: I am going to implement now a similar filter for diff command (I need 
both of them on my project), my idea is:

<filterchain>
    <diff reference="referenceFile.txt"/>
</filterchain>

It can be implemented in a similar way as <concat> Ant Task, nevertheless via 
filter is also a good solution for that. I am going to add a new patch for 
that (when I done it)
Comment 9 David Leal 2006-10-01 14:50:39 UTC
Antoine,

Please let me know if this patch (SortFilter) will be incorporated in a future 
Ant Release. Thanks. David
Comment 10 Stefan Bodewig 2009-08-26 06:42:35 UTC
svn revision 808009