按字母顺序对列表进行排序 [英] Sort List by alphabetical order

查看:107
本文介绍了按字母顺序对列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按字母顺序对列表进行排序,并尝试将我在javascript中拥有的内容移植起来.但这给了我一个关于String的异常,它没有实例方法'<'.我希望有人可以帮助我解决此问题.因为我不知道如何解决此问题.

I'm trying to sort a list by alphabetical order and tried porting something i had in javascript to flutter. But it gives me an exception on String that it does not have the instance method '<'. I hope someone can help me fix this. Because i have no clue how to correct this issue.

data.sort((a, b) {
            var aName = a['name'].toLowerCase();
            var bName = b['name'].toLowerCase();
            return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
        });

我收到此异常:

E/flutter (16823): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter (16823): NoSuchMethodError: Class 'String' has no instance method '<'.

推荐答案

<>通常是compareTo方法的快捷方式.

< and > is usually a shortcut to a compareTo method.

只需使用该方法即可.

data.sort((a, b) {
  return a['name'].toLowerCase().compareTo(b['name'].toLowerCase());
});

这篇关于按字母顺序对列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆