繁体   English   中英

如何使用Dart语言按字母顺序对字符串列表进行排序?

[英]How to sort a string list alphabetically in Dart language?

我有4家公司,每个公司都有10多家分支机构。 如果用户选择公司,则需要使用showDialog显示所选公司分支。 直到这里一切都按预期工作。 我的问题是我无法按字母顺序对List<String>排序。

任何想法如何在Dart中按字母顺序对字符串列表(列表)进行排序?

List<String> _myBranchListName;
  • Flutter是一个应用程序开发SDK

  • Dart是一种通用编程语言

  • Flutter应用程序是用Dart编写的

  • 所以:

    如何在Flutter List <String>中对A到Z进行排序?

  • 将会

    如何在Dart List <String>中按A到Z排序?

  • A到Z:按字母顺序被称为:

    如何在Dart List <String>中按字母顺序排序?

  • List <String>:是字符串类型的列表,因此

    如何在Dart字符串列表中按字母顺序排序?

  • 更多

    如何使用Dart语言按字母顺序对字符串列表进行排序?

码:

main() {
  List<String> _myBranchListName= ['k branch', 'a branch', 'f branch'];

  _myBranchListName.sort();
  print(_myBranchListName);

  //[a branch, f branch, k branch]
}

更新

我有一个自定义列表,menas列表包含class produt,product包含可变标题,现在我需要根据title简短列出我的列表''' 评论

排序方法:

根据比较功能指定的顺序对该列表进行排序。

如果省略了compare,则默认的List实现使用Comparable.compare。

比较器可以比较对象是否相等(返回零),

即使它们是不同的对象。 排序功能是

不能保证稳定,所以不同的对象

结果相等可能会以任何顺序发生

  • 您的代码(😊警告:我没有测试此代码,我需要您的反馈):

produts.sort((a, b) => a.title.compareTo(b.title));

List包含称为sort的方法,该函数将按字母顺序(从a到z)对列表进行排序。

我为您创建了函数,以使过程更加清晰:

List<String> sort(List<String> _myBranchListName){    // This function take List of strings and return it organized alphabetically
 // List<String> _myBranchListName = ["B branch", "C branch" , "A branch"]; // example of input
  print(_myBranchListName);   // will show the result in the run log
  _myBranchListName.sort();
  print(_myBranchListName);   // will show the result in the run log
  return _myBranchListName;
}

从技术上讲,您只需要_myBranchListName.sort()即可对数组进行排序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM