I have this object list List<ABC>. I would like to sort the list by the latest updated date. How can I achieve that?
v.sort((a, b) => a.updatedAt.compareTo(b.updatedAt));
I use above code to sort, but it not descending.
v.sort((a, b) => b.updatedAt.compareTo(a.updatedAt))
This fixed
v.sort((b, a) => a.updatedAt.compareTo(b.updatedAt));
Related
I have two lists : _bufferCarnetList and _deletedWords.
When the user hits OK: I want to remove from _bufferCarnetList the items contained in _deletedWords.
I have tried using the .remove method, but nothing happens.
_bufferCarnetList.remove(_deletedWords).
Is there a way to do it in a direct way, or should I iterate the list and remove each item separately ?
Use the removeWhere method:
_bufferCarnetList.removeWhere((e) => _deletedWords.contains(e));
I currently have a list of widgets
List<LessonItem> _lessons = [];
One Widget inside this list, added by the user should look like this
LessonItem('Bio', 'Mrs Pithan', 'A1.012', HourMinute('08', '00'),
HourMinute('11', '50'), Colors.green), //lesson, teacher, room, from, to, color
Now I want to sort the list by a specific property from the widgets. How can I do that?
Thanks for any help!
I would try with the sort method in dart.
You choose a property you want to sort by, and then define how any two elements will be compared.
Here I'm comparing each element by id (smaller id would en up first after sort):
_lessons.sort((LessonItem a, LessonItem b) => a.id - b.id);
In this case I'm sorting by name (with the compareTo() method from String):
_lessons.sort((LessonItem a, LessonItem b) => a.name.compareTo(b.name));
You can find more detailed info and some examples in:
dart documentation
and
this helpful post
I have a collection A of let's say 100 items. From that list I want to perform a where clause which can rule out let's say 20 of items.
Is there a way to use Select clause or something else on items in which I could use external method that returns 2 items.
I would need to end up with 160 objects from the original list.
What I currently have is
public List<A> ToAList(B item)
{
return new List<A> {new A(), new A()};
}
If I make this call
originalList.Where(x => true).Select(y => ToAList(y)).ToList();
I end up having a list of 80 (from pseudo example) two-item A lists instead of a list containing 160 objects A.
I am looking for a way to avoid loops. Just plain Select or AddRange trick that could result in one list.
You can use SelectMany:
originalList.Where(x => true).SelectMany(y => ToAList(y)).ToList();
I want to filter a List, and I only want to keep a string if the string contains .jpg,.jpeg or .png:
scala> var list = List[String]("a1.png","a2.amr","a3.png","a4.jpg","a5.jpeg","a6.mp4","a7.amr","a9.mov","a10.wmv")
list: List[String] = List(a1.png, a2.amr, a3.png, a4.jpg, a5.jpeg, a6.mp4, a7.amr, a9.mov, a10.wmv)
I am not finding that .contains will help me!
Required output:
List("a1.png","a3.png","a4.jpg","a5.jpeg")
Use filter method.
list.filter( name => name.contains(pattern1) || name.contains(pattern2) )
If you have undefined amount of extentions:
val extensions = List("jpg", "png")
list.filter( p => extensions.exists(e => p.matches(s".*\\.$e$$")))
To select anything that contains one of an arbitrary number of extensions:
list.filter(p => extensions.exists(e => p.contains(e)))
Which is what #SergeyLagutin said above, but I thought I'd point out it doesn't need matches.
Why not use filter() with an appropriate function performing your selection/predicate?
e.g.
list.filter(x => x.endsWith(".jpg") || x.endsWith(".jpeg")
etc.
Sorry if this is a straight forward thing, I'm new to Groovy. I'm trying to figure out how to sort this list on the "uses" key in each sub list, but I can't seem to get it figured out:
[[name:foo, uses:2], [name:bar, uses:1], [name:baz, uses:4]]
I'm hoping to get the following result:
[[name:baz, uses:4], [name:foo, uses:2], [name:bar, uses:1]]
Does someone out there know how to best handle this? I checked similar questions but couldn't find anything pertaining to Groovy.
Thanks in advance.
The easiest way to do it would be to use the sort method
def sorted = lists.sort( {a, b -> b["uses"] <=> a["uses"] } )
sorted.each {
println it
}
// prints
// [name:baz, uses:4]
// [name:foo, uses:2]
// [name:bar, uses:1]
I think I figured it out...
sort{a,b -> b['uses'] <=> a['uses']}
...seems to do the trick.
just wanted to add a shorter version
[[name:'foo', uses:2], [name:'bar', uses:1], [name:'baz', uses:4]].sort{-it.uses}