0Pricing
Dart Academy · Lesson

Searching and Sorting Lists

Find, contains, indexOf, and sort.

Searching and Sorting Lists is a free Dart Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Finding Things

Lists give you quick ways to ask questions about their contents, like whether an item exists or where it sits. Let us explore searching. 🔍

Does It Contain?

The contains method returns true or false, telling you whether a value lives somewhere in the list.

var pets = ['cat', 'dog'];
print(pets.contains('dog')); // true

Where Is It?

Use indexOf to find an item's position. It returns the first matching index, or -1 when the value is missing.

print(pets.indexOf('dog')); // 1

Missing Means Minus One

When indexOf cannot find your value, it returns -1. Checking for -1 is a simple way to handle absent items.

print(pets.indexOf('fish')); // -1

First Match by Rule

Search with a condition using firstWhere. It returns the first item that passes your test function.

var nums = [3, 8, 5];
print(nums.firstWhere((n) => n > 4)); // 8

Any and Every

Ask broad questions fast. any is true if at least one item matches, while every is true only when all of them do.

print(nums.any((n) => n > 7)); // true

Sort In Place

The sort method reorders the list itself, from smallest to largest by default. It changes the original, returning nothing.

var scores = [3, 1, 2];
scores.sort();
print(scores); // [1, 2, 3]

Sort Strings

Calling sort on text orders it alphabetically by default, comparing characters from left to right.

var names = ['Bea', 'Al', 'Cy'];
names.sort(); // [Al, Bea, Cy]

Custom Sort Order

Pass a comparator to sort by your own rule. Return a negative number to put a before b, positive to flip them.

scores.sort((a, b) => b - a); // descending

Reverse a List

The reversed getter gives a flipped view of the list. Wrap it in toList when you need a real reversed list back.

print(scores.reversed.toList());

lastIndexOf

When duplicates exist, lastIndexOf finds the final matching position instead of the first one.

var bits = [1, 0, 1];
print(bits.lastIndexOf(1)); // 2

Quick Check

You call indexOf for a value that is not present. What comes back?

Recap

You can now interrogate and order lists: contains, indexOf, firstWhere, any, every, and sort with custom rules. Data wrangling unlocked! 📊

Frequently asked questions

Is the “Searching and Sorting Lists” lesson free?

Yes — the full text of “Searching and Sorting Lists” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.

What will I learn in “Searching and Sorting Lists”?

Find, contains, indexOf, and sort. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Dart Academy?

No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Searching and Sorting Lists” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Dart Academy lesson?

Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Creating Lists and Indexing
  2. Adding, Removing, and Updating Items
  3. Searching and Sorting Lists
  4. Transforming With map, where, and toList
← Back to Dart Academy