Sort Tool with Custom Objects by date - sorting

I have a list of TestRecord objects in velocity which have a method getDateExecuted which returns a java.util.date object. I would like to sort the Records by date using the sort tool.(or appropriate sorting algorithm) However I have not seen any implementations of the sort tool that aren't using an xPath node as the list argument. If the sort tool can not be used any information on creating a bubble/selection sort using only foreach loops would be helpful.
sort tool documentation:
http://velocity.apache.org/tools/devel/apidocs/org/apache/velocity/tools/generic/SortTool.html
http://www.hannonhill.com/kb/Script-Formats/#sort-tool

Add the SortTool object to the context:
context.put("sortTool", new SortTool());
In the template, you can get the sorted list (sorted by dateExecuted) as :
#foreach($testRecord in $sortTool.sort($testRecordList, "dateExecuted:asc"))
$testRecord.dateExecuted
#end
It is clearly specified in the SortTool documentation
http://velocity.apache.org/tools/devel/apidocs/org/apache/velocity/tools/generic/SortTool.html
Let me know for any issue

Related

Build list from nested object in apache freemarker?

I have a list of object and I want to get a list from its field.
Say I pass List<Auto> autos from java side to the template on the document side. Each Auto has a field of speed.
So the result should be list of speed.
I can do it manually looping through the autos and building a new list from the speed fields.
Is there any easier solution built in for this in the freemarker something like 'autos.speed?tolist'
You are looking for the sequence built-in map, which is available from version 2.3.29. It returns a new sequence where all elements are replaced with the result of the parameter lambda, function, or method. This allows you to do:
autos?map(auto -> auto.speed)
If you want to do this in Java, see:
Create list of object from another using Java 8 Streams
There it comes down to:
autos.stream().map(Auto::getSpeed).collect(Collectors.toList());

How to get list of objects from s3 bucket sorted by last modified timestamp using minio-go api?

I went through the documentation of minio-go-api. But didn't get any solution for that, as objects are sorted based on the alphabetic order.
A hack way, will be to first read all the objects and then take last modified date from each object and form the new list, which is not at all feasible for production
#Siddhanta Rath, One way to handle this is to use mc tool. Command mc find --newer and mc find --older will handle this. But internally, it will do listObjects and do the sorting for you.
The other approach would be to subscribe to notification and make sure that there is a list of uploaded objects in a database.
There is no capability to specify sort order in the Amazon S3 API. Your application will need to sort the objects into the desired oder.

How to sort an array values in `desc` in computed?

I have an model value, when i do the each iteration it works fine.
<ul>
<li>See here : </li>
{{#each selectedCreditCard.balances.tenures as |balance|}}
<li>Balances is : {{balance}}</li>
{{/each}}
</ul>
But I require to sorted the value by desc way. so I use the computed method to do the desc the array.
sortTenuresBy:['desc'],
sortedTenures: Ember.computed.sort('selectedCreditCard.balances.tenures', 'sortTenuresBy'),
maxTenure:Ember.computed(function(){
return this.get('sortedTenures').get('firstObject');
But getting error as like this:
Assertion Failed: When using #each to observe the array 3,8,12,24, the array must return an object
how to fix this? please help me
If you look at API definition for Ember.computed.sort; it requires a property key (that is selectedCreditCard.balances.tenures in your case) and a sort definition (that is sortTenuresBy in your case). However, if you look at the examples provided; the sort definition must be either a plain property name or a property name followed by sort type such as name:desc or key:asc and so on. In summary; it is not possible to use Ember.computed.sort for plain arrays as in your case. I admit the API documentation is vague.
For your case; you have to either write the computed property as a function; which is what you do not want I suppose; because it is the common way; or you can make use of the following addon. What is great about ember-awesome-macros is you can nest the provided computed macros.
If you look at API for array.sort; it says "combines the functionality of both Array.prototype.sort() and Ember.computed.sort". Hence we can use this one. You want the array to be sorted in descending; I suppose something like the following
sortTenuresBy: array.reverse(array.sort('selectedCreditCard.balances.tenures'))
should work.

Solr Spatial: Is it possible to filter by one geolocation field and sort by a different one?

We would like to perform a spatial search on one geo field but distance sort the results based on a second geo field. It seems that Solr supports this for the LatLonType. Here we simply add parameters to the geodist function.
The geodist(param1,param2,param3) function supports (optional) parameters:
param1: the sfield
param2: the latitude (pt)
param3: the longitude (pt)
Unfortunately, this doesn't seem to work with the SpatialRecursivePrefixTreeFieldType. However, we have to use SpatialRecursivePrefixTreeFieldType since we have several locations for each document and this is not supported for the LatLonType. Is there any solution other than writing our own field type?
Finally I figured it out. However, the solution is a bit of a hack. I've now created a plugin jar that contains a modified version of the GeoDistValueSourceParser class. Within this class I've modified the method parseSfield to simply use a constant sfield, which should be used for sorting. Then I've hooked the class up by adding the line
<valueSourceParser name="customdist" class="bla.search.function.distance.CustomGeoDistValueSourceParser"/>
to the solrconfig.xml. So far I don't understand why the GeoDistValueSourceParser isn't configurable? It shouldn't be too difficult to write it in a way that a different geo field can be specified for sorting.

grails sort by date not working

I have domain object named Roll and on the list page i want to show the user all the Roll objects iterating through a list, sorted by entry date.
Here is the code i am using
[rollList: Roll.findAll(sort:"rollDate"){userid==uid}]
rollDate is a field inside the Roll object with data type java.util.Date
Any suggestion on why the output is not sorted by rollDate. When i iterate through the rollList on the gsp page it is not sorted.
Also, on the Roll domain object i have even put this code, but it's still not sorting.
static mapping = {
sort "rollDate"
}
Thank you.
Why aren't you using the dynamic finders?
Roll.findAllByUserid( uid, [ sort:"rollDate", order: 'desc'] )
should work.
The findAll( Map, Closure ) method appeared not a long time ago, perhaps it was not tested well...
You might need to use order in your query as well, then add order to it
[rollList: Roll.findAll(sort:"rollDate", order: 'desc'){userid==uid}]
After trying both the solutions mentioned it still didn't work. So i thought something might be wrong on the front end.
While researching more i found that since i was using jquery data tables it used to re order the sorting. The solutions i found was here
jQuery DataTable rows order
So both the answers above are correct. The issue was actually with jquery data tables.

Resources