[英]Flutter sort List of Objects after two values
I have a List
of CustomObjects
that I need to sort.我有一个需要排序的
CustomObjects
List
。 First the objects should be sorted after their property dateTime
and if that is the same, it should be sorted after another property, the compare
-property.首先,对象应该在它们的属性
dateTime
之后排序,如果相同,它应该在另一个属性之后排序,即compare
-property。
I searched for multisort and found this:我搜索了 multisort 并发现了这个:
medcimentNotificationListData.sort((med1, med2) {
var r = med1.datetime.compareTo(med2.datetime);
if (r != 0) return r;
return med1.mealTimeDescription.compareValue
.compareTo(med2.mealTimeDescription.compareValue);
});
But when printing the list right after it, the list is not sorted..但是当在它之后打印列表时,列表没有排序..
medcimentNotificationListData.forEach((medicamentNotificationData) {
print(
'${medicamentNotificationData.title}, order: ${medicamentNotificationData.mealTimeDescription.compareValue}');
});
What am I missing here?我在这里想念什么? Is there an easy way to multisort?
有没有简单的方法进行多选?
Let me know if you need any more info!如果您需要更多信息,请告诉我!
when you are calling the sort()
method the function calls (a, b) { // your function}
which should return either -1, 0 or 1. this function is called on the existing order.当您调用
sort()
方法时,function 调用(a, b) { // your function}
应该返回 -1、0 或 1。这个 function 在现有订单上被调用。 at first your element a
is your first element and element b
is second element of the list as the existing order of the list首先,您的元素
a
是您的第一个元素,元素b
是列表的第二个元素,作为列表的现有顺序
if your function returns -1 it means your element a
should be placed before the element b
therefore it places a
before the b
and call the function again by replacing older element b
as new element a
and new element b
will be the element after the old b
element.如果您的 function 返回 -1 这意味着您的元素
a
应该放在元素b
之前,因此它将a
放在b
之前并通过将旧元素b
替换为新元素a
和新元素b
将是旧元素之后的元素再次调用 function b
元素。
if your function returns 0 it means elements a
and b
are both same.如果您的 function 返回 0 这意味着元素
a
和b
都是相同的。 therefore it places a
before the b
and call the function again by replacing older element b
as new element a
.因此它将
a
放在b
之前,并通过将旧元素b
替换为新元素a
再次调用 function 。
but when your function returns the 1 it means your element a
is coming after the element b
.但是当您的 function 返回 1 时,这意味着您的元素
a
在元素b
之后。 therefore the function is called again by replacing element a
with the element before the old element a
.因此,通过将元素
a
替换为旧元素a
之前的元素,再次调用 function 。
Following code shows how this is works以下代码显示了这是如何工作的
final List<int> list = [1, 0, 3, 4, 2 , 6, 8, 2 , 5];
list.sort((a,b) {
print("a : $a, b : $b");
int result = a.compareTo(b);
print('result : $result \n');
return result;
});
output output
a : 1, b : 0
result : 1
a : 1, b : 3
result : -1
a : 3, b : 4
result : -1
a : 4, b : 2
result : 1
a : 3, b : 2
result : 1
a : 1, b : 2
result : -1
a : 4, b : 6
result : -1
a : 6, b : 8
result : -1
a : 8, b : 2
result : 1
a : 6, b : 2
result : 1
a : 4, b : 2
result : 1
a : 3, b : 2
result : 1
a : 2, b : 2
result : 0
a : 8, b : 5
result : 1
a : 6, b : 5
result : 1
a : 4, b : 5
result : -1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.