Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Wiki Markup
h2. Sort
*Available as of Camel 

Sort

...

2.0

...

*

Sort can be used to sort a message. Imagine you consume text files and before processing each file you want to be sure the content is sorted.

...



Sort will by default sort the body using a default comparator that handles numeric values or uses the string representation. You can provide your own comparator, and even an expression to return the value to be sorted. Sort requires the value returned from the expression evaluation is convertible to {{java.util.List}} as this is required by the JDK sort operation

...

Using from Java DSL

In the route below it will read the file content and tokenize by line breaks so each line can be sorted.

Code Block
.

h3. Options

{div:class=confluenceTableSmall}
|| Name || Default Value || Description ||
| {{comparatorRef}} | | Refers to a custom {{java.util.Comparator}} to use for sorting the message body. Camel will by default use a comparator which does a A..Z sorting. |
{div}

h3. Using from Java DSL
In the route below it will read the file content and tokenize by line breaks so each line can be sorted. 
{code}
from("file://inbox").sort(body().tokenize("\n")).to("bean:MyServiceBean.processLine");
{code}

You can pass in your own comparator as a 2nd argument:

...


{code
}
from("file://inbox").sort(body().tokenize("\n"), new MyReverseComparator()).to("bean:MyServiceBean.processLine");

Using from Spring DSL

In the route below it will read the file content and tokenize by line breaks so each line can be sorted.

Code Block
xmlxml
titleCamel 2.7 or better
{code}


h3. Using from Spring DSL
In the route below it will read the file content and tokenize by line breaks so each line can be sorted. 

{code:xml|title=Camel 2.7 or better}
<route>
  <from uri="file://inbox"/>
  <sort>
    <simple>body</simple>
  </sort>
  <beanRef ref="myServiceBean" method="processLine"/>
</route>
{code}

{code
:xml
xml
|title
=Camel 2.6 or older
}
<route>
  <from uri="file://inbox"/>
  <sort>
    <expression>
      <simple>body</simple>
    </expression>
  </sort>
  <beanRef ref="myServiceBean" method="processLine"/>
</route>
{code}

And to use our own comparator we can refer to it as a spring bean:

...


{code
:xml
xml
|title
=Camel 2.7 or better
}
<route>
  <from uri="file://inbox"/>
  <sort comparatorRef="myReverseComparator">
    <simple>body</simple>
  </sort>
  <beanRef ref="MyServiceBean" method="processLine"/>
</route>

<bean id="myReverseComparator" class="com.mycompany.MyReverseComparator"/>
{code}

{code
:xml
xml
|title
=Camel 2.6 or older
}
<route>
  <from uri="file://inbox"/>
  <sort comparatorRef="myReverseComparator">
    <expression>
      <simple>body</simple>
    </expression>
  </sort>
  <beanRef ref="MyServiceBean" method="processLine"/>
</route>

<bean id="myReverseComparator" class="com.mycompany.MyReverseComparator"/>

Besides <simple>, you can supply an expression using any language you like, so long as it returns a list.

...

{code}

Besides {{<simple>}}, you can supply an expression using any [language|Languages] you like, so long as it returns a list.

{include:Using This Pattern}