Alyce Brady, 2019, based on the previous Address Book project
Kalamazoo College, Kalamazoo, MI
In previous activities we have been developing an Art Catalog web page. A useful feature is the ability to sort the artworks alphabetically by artist name. In this activy, you will integrate a sorting function into the art catalog project.
A good starting point would be the sorting functions you have already implemented and tested.
Exercise 1 - Import Sorting Functions
Import the
selectionSort
,findMinIndex
, andswap
functions from the sandbox page you used for the Swap and Find Min and Selection Sort activities into your Art Catalog project.Just copy theselectionSort
,findMinIndex
, andswap
functions into your Art Catalog project. Do not copy thegeneratePage
function, nor anything from thebody
of your sandbox page.
As it stands, the sort function you just imported will not work on an array of art catalog entries. This is because we can't use the comparison operators "<" and ">" to directly compare two full art catalog objects. What would it mean for one art catalog object to be "less than" another? Alphabetical order of the artist names? Numerical order of the dates? JavaScript has no way of knowing which we want. Fortunately, this is easily addressed.
Exercise 2 - Sort Art Catalog Entries by Artist Name
- Rename the function
findMinIndex
tofindMinimumName
andselectionSort
tosortByName
. Modify thesortByName
function so that it callsfindMinimumName
. ModifyfindMinimumName
so that wherever it was referring to whole array entries, it will now refer to theartist
attributes of those entries. In other words, all references toarray[i]
becomearray[i].artist
(if those are the names you gave the array, the index, and the artist name attribute).
![]()
- Add a Sort button on your page where you want it to appear. (But not within a
script
section of your page; the code to create a button is HTML code.) This button should have the attributeonclick="sortByName(myArray); buildTable(myTable, myArray)"as well as appropriate name, id and value attributes. (Use the id of your table object (without quotation marks) instead ofmyTable
, and the name of the variable holding your array of catalog entries instead ofmyArray
.)Test your sort button. If everything is working as it should, clicking the button should cause the table of art catalog entries to be sorted alphabetically by artist name.
You may look at the page on grading criteria for all Art Catalog In-Class Activities to see the criteria used to grade the Art Catalog activities.
If you have time, you can get a head-start on one of the tasks in
Programming Project #3: sorting by date.
Define a function similar to sortByName
that sorts catalog
entries by date rather than by artist name, and create a second sorting
button so that a user has the option to sort the catalog by artist name or
by date.