Sort a list of different objects with the same property (timestamp) in Flutter/Dart - list

I want to sort by date a List which contains 2 different objects (ClassA and ClassB), with the same property timestamp "createdAt". I have tried this solution :
_list.sort((a, b) => a.createdAt.compareTo(b.createdAt));
It only works when _list contains a single type of objects (ClassA or ClassB) but not with both.
Anyone has an idea ?
Thank you.
Solution : create an abstract class with createdAt property and implement it on childs

I think the problem is that you have a list with dynamic type. Therefore I would recommend creating an abstract class that contains both information from ClassA and ClassB, so that the dart compiler understands.
List<Parent> _list = [
ClassA(DateTime(2020, 04, 04)),
ClassB(DateTime(2020, 03, 04)),
ClassA(DateTime(2020, 02, 04)),
ClassB(DateTime(2020, 01, 04))
];
_list.sort((a,b)=> b.createdAt.compareTo(a.createdAt));
abstract class Parent {
DateTime createdAt;
}
class ClassA implements Parent {
DateTime createdAt;
ClassA(this.createdAt);
}
class ClassB implements Parent {
DateTime createdAt;
ClassB(this.createdAt);
}
Here is also a CodePen where I could sort the list.
https://codepen.io/md-weber/pen/RwWaMgz

Related

How does this query method work?

#Query("MATCH (m:Movie)<-[r:ACTED_IN]-(a:Person) RETURN m,r,a LIMIT {limit}")
Collection<Movie> graph(#Param("limit") int limit);
For this query, it's returning "RETURN m, r, a", which is a full subgraph with 3 elements. Then why the return value of 'graph' method is a collection of "Movie" only? Where is the 'r, a' which is also returned.
I am trying to understanding the mechanism behind the scene.
It seems that you have a #RelationshipEntity defined in your class path but do not use it when defining #Relationships in the domain classes.
Sample:
#NodeEntity
class Pet {
// ...
}
#NodeEntity
class Person {
#Relationship(type = "HAS")
private List<Pet> pets;
// ...
}
#RelationshipEntity(type = "HAS")
class HasRelationship {
// ...
}
If Neo4j OGM, that acts behind the scenes of SDN, finds a relationship type, it looks for #RelationshipEntity first and if it finds them, tries to map the returned types back to the #NodeEntity. In this case OGM finds HasRelationship and wants to map it to the Person class. This fails because Person does only know of Pet and the objects get discarded.
Like my answer on GitHub.

Pre-constructor initialization

My problem is like this, I have a class named "Product" and another class named "Agriculture", the "Agriculture" class is inheriting the "Product" class.
When I summon the "Agriculture" constructor obviously the "Product" constructor is summoned first.
The question is, can I initialize one of the product's members via a set method first?
If you have:
class Product { ... };
class Agriculture : public Product { ...};
you can't escape the standard rule that the base object is constructed before the derived object. You have no chance to intervene in this order, nor set anything in Product before it's constructor starts.
Recommendation:
The best design for your need would be to foresee a Product constructor that takes as additional parameter(s) the value(s) that you want to set:
class Product {
string origin;
public:
Product () : origin("tbd") { }
Product (string withorigin) { ...}
void setOrigin (string myorigin) { origin=myorigin; }
};
class Agriculture : public Product {
public:
Agriculture () : Product ("Earth") { ...}
};
Workaround:
If such design would not fit your needs, the only thing you could imagine, would be to have a static member in Product. This member would then be independent of any Product, and could thus be set before an object is constructed.
class Product {
static string defaultCurrency;
string currency;
public:
Product () : currency(defaultCurrency) { ... }
static void setDefaultCurrency (string cur) { defaultCurrency=cur; }
};
class Agriculture : public Product { ... };
int main() {
Product::setDefaultCurrency("EUR");
Agriculture a1;
}
It's more error prone: the construction result depends on order of operations not related to the construction. This could be a problem for example in case of multithreading, if several threads construct objects at same moment.
Product constructor is called firstly, and you set some values inside this constructor. So why you still want to initialize one of the product's members via a set method first?

instance creating instances

How can I create an instance which creates as many instances as I want?
I think I have to create a class Manager for example and inside that class with an aggregation relationship to create the class name salary and bottles.
I want to create an instance of Manager which creates as many instances of bottle and salary I want. How can I do that?
It's called a factory and it looks something like:
class Factory {
Product create(int n);
// ...
}
class Product {
// ...
}
class Prod1 : public Product {
// ...
}
int main() {
Factory factory = Factory();
Product prod[10] = factory.create(10);
// ...
with create simply returning a Product object of some derived type. Of course, there's usually some context passed into the Factory::create function to hint at the type of Product you want.
Use pointers. You can have a pointer which points to as many instances as you want and new them whenever you want.

MVC 4 - String Custom Template messing with Enums

I have a custom template ~/Views/Shared/EditorTemplate/String.cshtml and it seems to be causing the Exception:
The model item passed into the dictionary is of type 'Proj.Models.EnumType', but this dictionary requires a model item of type 'System.String'.
It seems to only happen to Enums. It also goes away if I remove the template. The template doesn't seem to cause it, I don't think it's even making it that far. I can put ANYTHING in there and the exception is the same.
So... can I not use an #Html.EditorFor with a model with an enum if I have a custom template?
Some context:
Model:
namespace Proj.Models
{
public enum EnumType
{
A = 0,
B = 1,
C = 2,
}
public class Mod
{
[Required]
public String Name;
[Required]
public EnumType Letter;
}
}
View:
#model Proj.Models.Mod
#Html.EditorFor(m=>m) // Exception happens here
Here is what I found to work for me.
In your template, make sure you declare your model as nullable enum type. Then, in code, check to see if it has a value and based on that do appropriate formatting.
#inherits System.Web.Mvc.WebViewPage<Proj.Models.EnumType?>
#{
Proj.Models.EnumType v = Model.HasValue ? Model.Value : {*some-default-value*};
#Html.{*Your-formatter-here*};
}

Sort a list based on URL parameter (or sort nested domain model in query)

I'm sure there is a way to do this, but I'm really stuck on this one.
I have a domain model that connects to entities Foo and Bar in a many-to-many-relationship. Now when I want to list all Foos to a certain Bar, I do the query and get a lot of FooBar objects. I iterate through these objects and add all Foos to a list.
Like so:
def fooBarRelations = FooBar.findAllByBar bar
def fooList = []
fooBarRelations.each { fooList.add it.foo }
How can I sort the fooList based upon the parameters a g:sortableColumn adds to the url namely sort (the field to sort) and order.
I know you can pass the parameters to the query directly but I think this is not possible in my case?
So how can I either
Make one query without list iterating so I can pass in the sorting parameters OR
Sort my custom list based upon the sorting parameters?
Addition 1 (03/25/2012)
If I could to this ...
def fooBarRelations = FooBar.findAllByBar bar, [sort: 'foo.' + params.sort, order: params.order]
... the problem would be solved. But passing this to the query does not have any effect on the output. Is there any way I can sort a query by a sub-property?
If you really can't sort within the query itself. Then you need a list of lists.
List<List<Fields>> mylist;// where List<Fields> is a lists of the fields.
Then use a Comparator to sort your List> by the desired filed. Say your desired field is at index 3:
new Compare(List<Fields> L1, List<Fields> L2){
if(L1.get(3)>L2.get(3))
return -1;//etc.
UPATE BASED ON COMMENT:
say your entity is as follows
public class Entity{
String name, address, school;
Integer bankaccount;
//etc...
}
Then
public class WhereISort{
List<Entity> myList;
String mysorter;//mysorter can be declared here as static final
public WhereISort(){//maybe pass list in here or whatever
}
public Response myWebService(params..., String sorter){
mysorter=sorter;//mysorter can be declared here as static final
Collections.sort(myList, new Comparator() {
public int compare(Entity e1, Entity e2) {
if(mysorter.equalsIgnoreCase("name")){
return e1.getName().compareToIgnoreCase(e1.getName());
}else if(mysorter.equalsIgnoreCase("bankaccount")){
//your code here, etc.
}
}
});
}
}
Of course, the main point is using "mysorter" and the inner class "Comparator" to sort