Populate data like
[[vaccineName:Typhoid, dueDate:2016-08-01 00:00:00.0],
[vaccineName:MMR, dueDate:2016-03-01 00:00:00.0],
[vaccineName:Hepatitis A, dueDate:2023-09-01 00:00:00.0],
[vaccineName:IPV, dueDate:2016-02-01 00:00:00.0],
[vaccineName:DT, dueDate:2016-01-01 00:00:00.0],
[vaccineName:Influenza, dueDate:2015-12-01 00:00:00.0]]
Now how can sort data according to dueDate. I want show this data either ascending or descending..
the short answer is
list.sort{ it.dueDate }
Where do you have that data?
You can use sort method: Groovy sort
In your case it will be something like this:
list = list.sort{ a,b-> b.dueDate <=> a.dueDate }
Related
I just started with grails and would like to sort a list by descending date. I tried this:
<g:sortableColumn property="eventStartDate" defaultOrder="asc" title="${message(code: 'event.eventStartDate.label', default: 'Event Start Date')}" />
However this does not sort the list. The controller looks like this:
def list = {
params.max = Math.min(params.max ? params.int('max') : 10, 100)
[eventInstanceList: Event.list(params), eventInstanceTotal: Event.count()]
}
How can I sort by descending eventStartDate?
The reason it might not have worked is:
Event.list(params) retrieves everything.
You are sorting it in ascending order (asc) but you said you want to sort it in descending order (desc).
Solution:
Change the second line of your controller action to:
[eventInstanceList: Event.list(order: "desc"), eventInstanceTotal: Event.count()]
More info:
http://grails.github.io/grails-doc/2.2.1/ref/Domain%20Classes/list.html
Event.list(params) returns every thing in the domain object. It doesn't do any filtering. Use findAllBy* or findBy* or createCriteria instead.
Example:
Event.findAllByNameAndDate(params.get("name"), params.get("date"), [max: 10, order: "desc"])
More info:
grails domain class .list() method with params
I want to do something like the following:
r.db('mydb').table('tab').between(
['s', 0],
['s', 99999], {index: r.desc('s-t')})
but this is not a correct Rql query:
RqlCompileError: DESC may only be used as an argument to ORDERBY. in:
Can I safely use
r.db('mydb').table('tab').between(
['s', 0],
['s', 99999], {index: 's-t'}).orderBy({index: r.desc('s-t')})
Will it be executed optimally (using single read instead of reading all records and then sorting them?
Yes, if you chain a between command with an orderBy one (using the same index), it will be executed in an efficient way.
I am using a CDbCriteria with its own conditions, with & order clauses. However, the order i want to give to the elements in the array is way too complex to specify in the order clause.
The solution i have in mind consists of obtaining the active records with the defined criteria like this
$theModelsINeed = MyModel::model()->findAll($criteria);
and then rearrange the order from my php code. How can i do this? I mean, i know how to iterate through its elements, but i donĀ“t know if it is possible to actually change them.
I have been looking into this link about populating active records, but it seems quite complicated and maybe someone could have some better advice.
Thanks
There is nothing special about Yii's active records. The find family of methods will return an array of objects, and you can sort this array like any other array in PHP.
If you have complex sort criteria, this means that probably the best tool for this is usort. Since you will be dealing with objects, your user-defined comparison functions will look something like this:
function compare($x, $y)
{
// First sort criterion: $obj->Name
if ($x->Name != $y->Name) {
return $x->Name < $y->Name ? -1 : 1; // this is an ascending sort
}
// Second sort criterion: $obj->Age
if ($x->Age != $y->Age) {
return $x->Age < $y->Age ? 1 : -1; // this is a descending sort
}
// Add more criteria here
return 0; // if we get this far, the items are equal
}
If you do want to get an array as a result, you can use this method for fetching data that supports dbCriteria:
$model = MyModel::model()->myScope();
$model->dbCriteria->condition .= " AND date BETWEEN :d1 AND :d2";
$model->dbCriteria->order = 'field1 ASC, field2 DESC';
$model->dbCriteria->params = array(':d1'=>$d1, ':d2'=>$d2);
$theModelsINeed = $model->getCommandBuilder()
->createFindCommand($model->tableSchema, $model->dbCriteria)
->queryAll();
The above example shows using a defined scope and modifying the condition with named parameters.
If you don't need Active Record, you could also look into Query Builder, but the above method has worked pretty well for me when I want to use AR but need an array for my result.
I have a schema describing tf-idf values for words in various articles.
Its description looks like:
tfidf_relation: {word: chararray,id: bytearray,tfidf: double}
Here is an example of such data:
(cat,article_one,0.13515503603605478)
(cat,article_two,0.4054651081081644)
(dog,article_one,0.3662040962227032)
(apple,article_three,0.3662040962227032)
(orange,article_three,0.3662040962227032)
(parrot,article_one,0.13515503603605478)
(parrot,article_three,0.13515503603605478)
I want to get output in a form:
cat article_one 0.13515503603605478, article_two 0.4054651081081644
and so on.
The question is, how do I make a relation from this which contains the word field and a tuple of id and tfidf fields?
Someting like this:
X = FOREACH tfidf_relation GENERATE word, (id, tfidf);
doesn't work. What is the correct syntax for this?
Try this:
t = LOAD 'input/file' USING PigStorage(',') as (word: chararray,id: bytearray,tfidf: double);
u = group t by word;
dump u;
The output will be
(cat,{(cat,article_two,0.4054651081081644),(cat,article_one,0.13515503603605478)})
(dog,{(dog,article_one,0.3662040962227032)})
(apple,{(apple,article_three,0.3662040962227032)})
(orange,{(orange,article_three,0.366204096222703)})
(parrot,{(parrot,article_three,0.13515503603605478),
(parrot,article_one,0.13515503603605478)})
I hope this is what you are looking for.
X = FOREACH tfidf_relation GENERATE word, {(id, tfidf)};
This is probably what you need.
is there anyway to fetch only the rows of a table which has a concrete
value in a concrete field.
For example:
$24_people = $table->getFieldAndValue('age', 24);
I now i can do it with a query, but wouldn't you find useful this kind
of functions? or maybe is no possible or is not convenient because some
reason ?
Regards
Javi
You mean something like this:
$table->createQuery()->where('age = ?', 24);
or:
$table->findAllByAge(24);
?
Yes.
$24_people = Doctrine::getTable('People')->findOneByAge(24);
See Doctrine magic methods.
http://www.symfony-project.org/doctrine/1_2/en/06-Working-With-Data