我有4个公司,每个公司都有10多个分支机构。如果用户选择了一个公司,我需要使用showDialog
显示所选的公司分支机构。到这里一切都按预期工作。我的问题是我无法按字母顺序排序List<String>
。
有什么办法可以在Dart中按字母顺序对字符串列表(List)进行排序吗?
List<String> _myBranchListName;
我有4个公司,每个公司都有10多个分支机构。如果用户选择了一个公司,我需要使用showDialog
显示所选的公司分支机构。到这里一切都按预期工作。我的问题是我无法按字母顺序排序List<String>
。
有什么办法可以在Dart中按字母顺序对字符串列表(List)进行排序吗?
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 String List中按字母顺序排序?
更多
如何在Dart语言中按字母顺序对字符串列表进行排序?
代码:
main() {
List<String> _myBranchListName= ['k branch', 'a branch', 'f branch'];
_myBranchListName.sort();
print(_myBranchListName);
//[a branch, f branch, k branch]
}
我有一个包含类“product”的自定义列表,其中“product”包含变量“title”,现在我需要根据标题对我的列表进行排序,如何处理?'''
sort方法:
根据比较函数指定的顺序对此列表进行排序。
如果省略了compare,则默认的List实现将使用Comparable.compare。
比较器可能会将对象视为相等(返回零),
即使它们是不同的对象。 sort功能不保证稳定,因此将作为相等进行比较的不同对象可能以任何顺序出现在结果中
produts.sort((a, b) => a.title.compareTo(b.title));
a.toLowerCase().compareTo(b.toLowerCase()
。 - stefanSList包含一个名为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()
来对数组进行排序。