58

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);
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Peter
  • 1,719
  • 4
  • 22
  • 31
  • What do you mean "random sort"? Are you trying to put the elements in random order? – beatgammit Nov 25 '12 at 18:39
  • Exactly, I want to put those elements in random order. – Peter Nov 25 '12 at 18:41
  • In that case, take a look at [shuffling algorithms](http://www.codinghorror.com/blog/2007/12/shuffling.html). I don't have time to come up with a full solution, but hopefully this helps. Also check out this [SO question](http://stackoverflow.com/questions/375351/most-efficient-way-to-randomly-sort-shuffle-a-list-of-integers-in-c-sharp) – beatgammit Nov 25 '12 at 18:44
  • Thx I'll look at this, if any one can come with a solution I will appreciate as well ! – Peter Nov 25 '12 at 18:47
  • 1
    Here's the bug requesting this feature, please star it to vote for it: http://code.google.com/p/dart/issues/detail?id=6788 – Seth Ladd Nov 26 '12 at 06:38
  • 2
    There is a List.shuffle method implemented in the List class since some months – Fox32 May 28 '14 at 08:05

3 Answers3

167

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]
)
Fox32
  • 13,126
  • 9
  • 50
  • 71
38

Here is a basic shuffle function. Note that the resulting shuffle is not cryptographically strong. It uses Dart's Random class, which produces pseudorandom data not suitable for cryptographic use.

import 'dart:math';

List shuffle(List items) {
  var random = new Random();

  // Go through all elements.
  for (var i = items.length - 1; i > 0; i--) {

    // Pick a pseudorandom number according to the list length
    var n = random.nextInt(i + 1);

    var temp = items[i];
    items[i] = items[n];
    items[n] = temp;
  }

  return items;
}

main() {
  var items = ['foo', 'bar', 'baz', 'qux'];

  print(shuffle(items));
}
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Kai Sellgren
  • 27,954
  • 10
  • 75
  • 87
5

You can use shuffle() with 2 dots like Vinoth Vino said.

List cities = ["Ankara","London","Paris"];

List mixed = cities..shuffle();

print(mixed);

// [London, Paris, Ankara]
Kasım Onuk
  • 324
  • 3
  • 6