← Data apps

Search, filter and sort

Find songs in a bigger playlist: sort the table by title or by length, search the titles for some typed text, and filter the short songs. Searching and filtering both use the same algorithm: go through every row and copy the ones that match into a second table, so the original songs stay as they are.

Every app with a list has a search box and a sort button. Underneath, a search is a loop that checks every row.

The playlist has more songs now. Today you'll help a user find one:

That second idea is an algorithm: a set of steps that works for any table. Search is the same steps with a different test.

The Find a song app: a search box with Search, Short songs, Sort by title and Longest first buttons, a table of all songs and a table of found songs.

How this lesson works: See it, Build, Check, Make it yours
Extra examplesYour teacher may tell you which.
See itTry the finished thing first, so you know what you are building.

Try the finished app

This is the app you'll build, open on the Run tab. Sort both ways, then type an and click Search, then click Short songs.

Try it yourselfYour edits are saved in this browser.

BuildWatch the short clip, then do the steps. Your app carries on from the step before.

Build 1: sort

  1. Add an event: SortTitleButton is clicked → Tables, Sort table Songs by title, A to Z, smallest first.
  2. Add an event: SortLengthButton is clicked → sort Songs by length, Z to A, biggest first.
  3. Run it and try both. Do the artists stay with their songs?
Sort by a field · Sound on for the voiceover
Try it yourselfYour edits are saved in this browser.

Sort by title sorts Songs by title A to Z; Longest first sorts Songs by length, biggest first.

    Run your program. This ticks when your code does all of the above.

    BuildWatch the short clip, then do the steps. Your app carries on from the step before.

    Build 2: search

    The Results table has the same fields as Songs, and ResultsTable shows it.

    1. Add an event: SearchButton is clicked → Clear table Results (so old results don't stay).
    2. Give it a sub-event: For each row in table Songs, and a second condition Compare contains(lower(LoopItem.title), lower(SearchInput.text)) = true. lower(...) makes capitals not matter.
    3. The sub-event's action: Add a row to table Results, with title LoopItem.title, artist LoopItem.artist, length LoopItem.length.
    4. Run it. Search for an, then o. Is All songs still complete?
    Search every row · Sound on for the voiceover
    Try it yourselfYour edits are saved in this browser.

    Search clears Results, then for each row of Songs whose title contains the typed text, adds that row to Results.

      Run your program. This ticks when your code does all of the above.

      BuildWatch the short clip, then do the steps. Your app carries on from the step before.

      Build 3: filter the short songs

      Build Short songs the same way as Search. Only the test changes: Compare LoopItem.length ≤ 3.

      Try it yourselfYour edits are saved in this browser.

      Short songs clears Results, then adds every row of Songs with a length of 3 or less.

        Run your program. This ticks when your code does all of the above.

        CheckA quick check that the idea has stuck.

        Check: keep the songs safe

        After Nib's Short songs, the long songs are gone for good. Click the line where Nib goes wrong.

        Suppose length were a text field. Sorting the lengths 10, 9 and 3 A to Z would give which order?

        Make it yoursChange your app on purpose, then try it on someone.

        Make it yours: search artists, show all

        Change Search's test so it looks in the title and the artist. One way: contains(lower(LoopItem.title & " " & LoopItem.artist), lower(SearchInput.text))

        There's a Show all button. Make it copy every song into Results. Which part of the pattern does it not need?

        Try it yourselfYour edits are saved in this browser.

        Search also finds songs whose artist contains the typed text.

          Run your program. This ticks when your code does all of the above.

          Show all clears Results and copies every row of Songs into it.

            Run your program. This ticks when your code does all of the above.

            Make it yours Extra · typicalChange your app on purpose, then try it on someone.

            Extra: play the clicked song

            Songs now has a sound field naming one of App Builder's built-in sounds (calm, upbeat, sleepy, beat), and there's a Sound object called Music under the tables.

            1. Add an event: An item in SongTable is clicked.
            2. Action: Set Music sound to the one named ClickedItem.sound.
            3. Action: Play Music.
            4. Run it and click a few songs. Search still copies the sound field into Results, so a found song plays too if you do the same for ResultsTable.

            The sounds are short tracks made for App Builder, free to use. A browser only plays sound after you've clicked something in the app, which a row click is.

            Try it yourselfYour edits are saved in this browser.

            Clicking a song in SongTable sets Music's sound to that row's sound field, then plays it.

              Run your program. This ticks when your code does all of the above.

              Now you can
              Next lesson7. Challenge: your own data appPlan, build and test a data app of your own for a real user: a reading log, sport stats, a collection, a practice log. It keeps records in a table that's remembered between visits, and helps the user find what they need with search, a filter, a sort or a total.
              For teachers

              Students sort the Songs table by a field (titles A to Z, longest first), then build search and filter with one algorithm: clear a Results table, go through every row of Songs with For each row, and copy the rows whose test passes. The Check finds Nib removing the non-matching rows (data lost) and shows why numbers kept as text sort wrongly. Make it yours: search two fields, and Show all. About 60 minutes.

              Open the teacher guide: answers, solutions, slides and a worksheet.

              Students will be able to

              Before class

              Search and filter are the same algorithm (a linear search: test every row, keep the matches), with a different test. Name it as an algorithm students will meet again in Python and SQL.

              Results starts empty and is cleared at the start of each search, so old results don't stay (the same idea as list-clear-before-add, the other way round).

              App Builder's Sort is a tool, not an algorithm students write: it sorts numbers by size, text A to Z ignoring capitals, and keeps equal rows in their order.

              Running the lesson

              1. Before Build 2 (whole class): find every song with 'o' in the title on a printed list. How did you do it? That's the loop.
              2. Check: Nib's filter is worth acting out: remove the long songs from the class list. Where did they go?
              3. Sort question: write 10, 9 and 3 on cards as words and sort them like a dictionary.

              The hard part (on purpose)

              The search condition: contains(lower(LoopItem.title), lower(SearchInput.text)) = true is long. Build it in parts in the Find expressions browser, and test with a single letter.

              When students get stuck

              Stretch and support

              Stretch: Search with a minimum length too: two conditions in the same sub-event.

              Support: Build Search together; pairs build Short songs by changing only the test.

              Where it leads

              Next: the challenge, a data app of their own.

              What each step is for