Shuffling a List
Part of the Logic & Flow section of Coddy's Dart journey — lesson 8 of 65.
When you need to randomize the order of elements in a list, Dart provides the shuffle() method. This method rearranges all elements in the list randomly, creating an unpredictable order that's perfect for games, simulations, or any situation where you need random arrangements.
The shuffle() method modifies the original list directly, changing the positions of all elements randomly. Each time you call shuffle(), you'll get a different arrangement of the same elements.
List<String> cards = ['Ace', 'King', 'Queen', 'Jack'];
print('Original: $cards'); // [Ace, King, Queen, Jack]
cards.shuffle();
print('Shuffled: $cards'); // [Queen, Ace, Jack, King] (random order)This method is particularly useful for creating card games, randomizing quiz questions, or any application where you want to present items in an unpredictable sequence. Since shuffle() works in place, you don't need to create a new list - the existing list is randomly reorganized for you.
Challenge
EasyCreate a program that manages a game tournament bracket by shuffling teams into random matchups. Your program should:
- Read a string input representing the tournament name (this value is read but not printed)
- Read multiple string inputs representing team names (the input ends when an empty string is received)
- Shuffle the teams into a random order
- Print each team name on a separate line
For example, if the tournament name is "Local Cup" and the teams are "Wolves", "Hawks", your program should output the team names in a shuffled (random) order, one per line:
Hawks
WolvesOr:
Wolves
HawksSince shuffle() randomizes the order, any arrangement of the teams is valid.
Cheat sheet
The shuffle() method randomizes the order of elements in a list:
List<String> cards = ['Ace', 'King', 'Queen', 'Jack'];
print('Original: $cards'); // [Ace, King, Queen, Jack]
cards.shuffle();
print('Shuffled: $cards'); // [Queen, Ace, Jack, King] (random order)The shuffle() method modifies the original list directly and creates a different random arrangement each time it's called.
Try it yourself
import 'dart:io';
void main() {
// Read tournament name
String? tournamentName = stdin.readLineSync();
// Read team names until empty string
List<String> teams = [];
String? teamName;
while ((teamName = stdin.readLineSync()) != null && teamName!.isNotEmpty) {
teams.add(teamName);
}
// TODO: Shuffle the teams list for random matchups
// Print each team on a separate line
for (String team in teams) {
print(team);
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced List Manipulation
List Properties: first & lastList State: isEmpty & isNotEmpReversing a ListAdding to a List: insertList Removal: removeWhereFinding in a List: indexOfSorting a ListShuffling a ListRecap - List Organizer4Advanced Map Manipulation
Iterating Over a MapChecking for Keys and ValuesMap Properties: keys & valuesConditional Add: putIfAbsentRemoving Entries from a MapNested MapsRecap - Inventory Update2Functional List Operations
Transforming with 'map'Filtering with 'where'Using '.toList()'Checking Conditions with 'any'Conditions with 'every'Finding with 'firstWhere'Recap - Data Filtering5Project: Shopping Cart Calc
Project SetupAdding Items to the Cart3Sets
What is a Set?Creating a SetAdding and Removing from SetsChecking for Elements in a SetConverting a List to a SetSet UnionSet IntersectionSet DifferenceRecap - Unique Guest List