Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
694 views
in Technique[技术] by (71.8m points)

sorting - List.shuffle() in Dart?

I'm looking every where on the web (dart website, stackoverflow, forums, etc), and I can't find my answer.

So there is my problem: I need to write a function, that print a random sort of a list, witch is provided as an argument. : In dart as well.

I try with maps, with Sets, with list ... I try the method with assert, with sort, I look at random method with Math on dart librabry ... nothing can do what I wana do.

Can some one help me with this?

Here some draft:

var element03 = query('#exercice03');
  var uneliste03 = {'01':'Jean', '02':'Maximilien', '03':'Brigitte', '04':'Sonia', '05':'Jean-Pierre', '06':'Sandra'};
  var alluneliste03 = new Map.from(uneliste03);
  assert(uneliste03 != alluneliste03);
  print(alluneliste03);

  var ingredients = new Set();
  ingredients.addAll(['Jean', 'Maximilien', 'Brigitte', 'Sonia', 'Jean-Pierre', 'Sandra']);
  var alluneliste03 = new Map.from(ingredients);
  assert(ingredients != alluneliste03);
  //assert(ingredients.length == 4);

  print(ingredients);

  var fruits = <String>['bananas', 'apples', 'oranges'];
  fruits.sort();
  print(fruits);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is a shuffle method in the List class. The methods shuffles the list in place. You can call it without an argument or provide a random number generator instance:

var list = ['a', 'b', 'c', 'd'];

list.shuffle();

print('$list');

The collection package comes with a shuffle function/extension that also supports specifying a sub range to shuffle:

void shuffle (
  List list,
  [int start = 0,
  int end]
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...