Search code examples
listdartiterable

List methods returns Iterable. Does it loses the order after applying a that methods?


I would like to keep the initial order while I apply different transformations to the items.

For example, I have a list that is ordered and I would like to apply a function using map to each element. This is my sample code:

List<int> numbers = [5,2,3,7];
numbers.sort((a, b) => a > b);
Iterable<int> numbersIncreased = numbers.map((e) => e + 3);

After the map, we get an Iterable. Does it keeps the order?

I would like to keep the initial order while I apply functions.


Solution

  • Yes, it does keep the order.

    And if you are unaware, you can simply call Iterable.toList() method to convert iterable back to List and see the order yourself.

    final updatedList = numbersIncreased.toList();
    print(updatedList);
    

    Hope this helps :)