我有下面的Map
Map<String, String> Dictionary = {
'37': 'thirty-four',
'94': 'ninety',
'91': 'ninety-one',
'22': 'twenty-one',
'61': 'sixty-one',
'9': 'nine',
'3': 'three',
'8': 'eight',
'80': 'eighty',
'81': 'eighty-one',
'Ninety-eight': '98',
'nine-hundred': '900'};
我想按升序排序,这样最终结果是
3: three
8: eight
9: nine
22: twenty-two
37: thirty-seven
61: sixty-one
80: eighty
81: eighty-one
91: ninety-one
94: ninety four
Ninety-eight': 98
nine-hundred': 900
但是我得到的结果是
22: twenty-two
3: three
37: thirty-seven
61: sixty-one
8: eight
80: eighty
81: eighty-one
9: nine
91: ninety-one
94: ninety-four
Ninety-eight: 98
nine-hundred: 900
以上结果的代码如下
sortDict(){
var sortedByKeyMap = Map.fromEntries(
Dictionary.entries.toList()..sort((e1, e2) => (e1.key).compareTo((e2.key))));
print(sortedByKeyMap);
}
你知道如何先将数字字符串键转换为int吗?
1条答案
按热度按时间ewm0tg9j1#
一个小技巧会帮到你,
输出: