Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 如何以字符串格式按日期对对象列表进行排序?_List_Sorting_Flutter_Dart_Flutter Web - Fatal编程技术网

List 如何以字符串格式按日期对对象列表进行排序?

List 如何以字符串格式按日期对对象列表进行排序?,list,sorting,flutter,dart,flutter-web,List,Sorting,Flutter,Dart,Flutter Web,我有一个列表,它有一个名为updatedAt的参数,它是一个包含DateTime.now().toString().substring(0,19)的字符串,我需要按日期(最新的(位置0)组织到最旧的(最后的位置)),但是当我试图使用List.sort((a,b)=>a.updatedAt.compareTo(b.updatedAt))时,我有以下错误,我是否必须将字符串转换为DateTime或其他格式 class Product { String id; String image;

我有一个列表,它有一个名为updatedAt的参数,它是一个包含
DateTime.now().toString().substring(0,19)
的字符串,我需要按日期(最新的(位置0)组织到最旧的(最后的位置)),但是当我试图使用
List.sort((a,b)=>a.updatedAt.compareTo(b.updatedAt))
时,我有以下错误,我是否必须将字符串转换为DateTime或其他格式

class Product {

  String id;
  String image;
  String name;
  String desc;
  int price;
  int quantity;
  String category;
  String createdAt;
  String updatedAt;
  bool active;

  Product({this.id, this.image, this.name, this.desc, this.price, this.quantity,
    this.category, this.createdAt, this.updatedAt, this.active}){
    quantity = 1;
    active = true;
  }

  Future<void> getProducts() async {

        List<Product> products = [];

        products.addAll(await getProductsFromFirestore()); // Here I add products from Firebase...

        print(products[0].updatedAt); // 2020-07-21 12:31:08
        print(products[1].updatedAt); // 2020-07-21 12:31:13
    
        products.sort((a, b) => a.updatedAt.compareTo(b.updatedAt));

  }
类产品{
字符串id;
字符串图像;
字符串名;
字符串描述;
国际价格;
整数;
字符串类别;
字符串createdAt;
字符串更新数据;
布尔活跃;
产品({this.id,this.image,this.name,this.desc,this.price,this.quantity,
this.category、this.createdAt、this.updatedAt、this.active}){
数量=1;
主动=真;
}
Future getProducts()异步{
列出产品=[];
products.addAll(等待getProductsFromFirestore());//这里我添加了来自Firebase的产品。。。
打印(产品[0].更新日期);/2020-07-21 12:31:08
打印(产品[1].更新日期);/2020-07-21 12:31:13
产品分类((a,b)=>a.updatedAt.compareTo(b.updatedAt));
}
错误(颤振腹板):

Error: Expected a value of type 'Comparable<dynamic>', but got one of type 'Product'
at Object.throw_ [as throw] (http://localhost:53122/dart_sdk.js:4339:11)
at Object.castError (http://localhost:53122/dart_sdk.js:4310:15)
at Object.cast [as as] (http://localhost:53122/dart_sdk.js:4626:17)
at Function.as_C [as as] (http://localhost:53122/dart_sdk.js:4271:19)
at http://localhost:53122/dart_sdk.js:15079:98
at Function._insertionSort (http://localhost:53122/dart_sdk.js:23099:55)
at Function._doSort (http://localhost:53122/dart_sdk.js:23086:24)
at Function.sort (http://localhost:53122/dart_sdk.js:23068:22)
at Array.[dartx.sort] (http://localhost:53122/dart_sdk.js:15079:26)
at RxList.new.sort (http://localhost:53122/packages/get/src/rx/rx_impl.dart.lib.js:610:27)
at home_controller.HomeController.new.getProductsFromController (http://localhost:53122/packages/site_catalogo_modelo/controllers/home_controller.dart.lib.js:94:21)
at product_controller.ProductController.new.getProducts (http://localhost:53122/packages/site_catalogo_modelo/controllers/home_controller.dart.lib.js:212:74)
at getProducts.next (<anonymous>)
at http://localhost:53122/dart_sdk.js:37340:33
at _RootZone.runUnary (http://localhost:53122/dart_sdk.js:37194:58)
at _FutureListener.thenAwait.handleValue (http://localhost:53122/dart_sdk.js:32178:29)
at handleValueCallback (http://localhost:53122/dart_sdk.js:32725:49)
at Function._propagateToListeners (http://localhost:53122/dart_sdk.js:32763:17)
at _Future.new.[_completeWithValue] (http://localhost:53122/dart_sdk.js:32606:23)
at async._AsyncCallbackEntry.new.callback (http://localhost:53122/dart_sdk.js:32628:35)
at Object._microtaskLoop (http://localhost:53122/dart_sdk.js:37455:13)
at _startMicrotaskLoop (http://localhost:53122/dart_sdk.js:37461:13)
at http://localhost:53122/dart_sdk.js:32980:9
错误:应为“Compariable”类型的值,但得到了“Product”类型的值
at Object.throw uu[as throw](http://localhost:53122/dart_sdk.js:4339:11)
在Object.castError(http://localhost:53122/dart_sdk.js:4310:15)
at Object.cast[作为](http://localhost:53122/dart_sdk.js:4626:17)
at Function.as_C[as as](http://localhost:53122/dart_sdk.js:4271:19)
在http://localhost:53122/dart_sdk.js:15079:98
at函数。\u insertionSort(http://localhost:53122/dart_sdk.js:23099:55)
在功能上(http://localhost:53122/dart_sdk.js:23086:24)
at Function.sort(http://localhost:53122/dart_sdk.js:23068:22)
在数组中。[dartx.sort](http://localhost:53122/dart_sdk.js:15079:26)
在RxList.new.sort(http://localhost:53122/packages/get/src/rx/rx_impl.dart.lib.js:610:27)
在家\u controller.HomeController.new.getProductsFromController(http://localhost:53122/packages/site_catalogo_modelo/controllers/home_controller.dart.lib.js:94:21)
在product_controller.ProductController.new.getProducts(http://localhost:53122/packages/site_catalogo_modelo/controllers/home_controller.dart.lib.js:212:74)
在getProducts.next()上
在http://localhost:53122/dart_sdk.js:37340:33
at_RootZone.runUnary(http://localhost:53122/dart_sdk.js:37194:58)
在未来的聆听者。然后等待。handleValue(http://localhost:53122/dart_sdk.js:32178:29)
在handleValueCallback(http://localhost:53122/dart_sdk.js:32725:49)
at函数。\u传播到侦听器(http://localhost:53122/dart_sdk.js:32763:17)
在_Future.new.[u completeWithValue](http://localhost:53122/dart_sdk.js:32606:23)
异步时。\u AsyncCallbackEntry.new.callback(http://localhost:53122/dart_sdk.js:32628:35)
在对象上。\u微任务循环(http://localhost:53122/dart_sdk.js:37455:13)
at_startMicrotaskLoop(http://localhost:53122/dart_sdk.js:37461:13)
在http://localhost:53122/dart_sdk.js:32980:9

问题是我没有使用列表,我使用的是状态管理,我的产品列表是一个可观察的,它的列表在另一个变量中,因此排序没有正常工作,而且我必须更改为“b.updatedAt.compareTo(a.updatedAt)”)'按我需要的顺序获取列表。

你能给我完整的代码吗,比如你存储的全值产品,还有,你为什么不试试这样做
products.sort()
看看这是不是你想要的?我觉得很有意思,我们可以一起做,再给我一些数据:)我看不出所提供的代码有任何问题;。你确定这是相关的代码,并且你复制并粘贴了它吗?我只是更新了代码,添加了整个类并模拟了过程,I j必须将firestore中的数据添加到产品列表中,问题是当我试图比较这两个日期时间子字符串时。@Alok
products.sort()
不起作用,因为
产品
没有实现
可比
接口。@djalmafreestyler
字符串
已经实现了
可比
接口。你的问题在别处。请创建一个最小的、可复制的示例。这并不意味着你需要显示你的Firestore代码,而是告诉我需要制作简化示例的ans(例如,与我链接的DartPad示例进行比较)。