Computer Science Canada Insertion Sort For Words |
Author: | Civik [ Wed Feb 27, 2008 1:14 pm ] | ||
Post subject: | Insertion Sort For Words | ||
Im trying to make a sort that sorts a list into alphabetical order, but also moves the other pieces of data that fit with the word thats being organized to be moved to the same location. Basically what i mean is i am making a song database and i want to organize by name, but i want the artist, album, and year to also be moved to the same spot that the name is put to, so if the name goes to spot 5 the other info gets moved to spot 5 as well. I have got the sort started based on other sorts on the site, im having problems though. As of right now im stuck. Heres my code :
|
Author: | Tony [ Wed Feb 27, 2008 1:20 pm ] |
Post subject: | RE:Insertion Sort For Words |
the problem is that right now you are sorting only the artist entries, but what you want to do is to sort the entire musicrecord records. a simple solution would be to shift records around within the musiclibrary array. Though if you are comfortable enough with arrays, it might make more sense to keep the library intact, and sort just the order indexes. |
Author: | Civik [ Wed Feb 27, 2008 1:22 pm ] |
Post subject: | Re: Insertion Sort For Words |
Is it easier to just sort the whole record at once? How would i go about that? Ive been trying for a few days to get this to work so im down to try whatever. |
Author: | Clayton [ Wed Feb 27, 2008 1:27 pm ] | ||||
Post subject: | RE:Insertion Sort For Words | ||||
Instead of sorting just artists, sort the entire record. Your swap would change from this:
To:
See the difference? Just be sure that you are still comparing the fields of the record against each other, so that you can still sort by some value. |
Author: | Civik [ Wed Feb 27, 2008 1:32 pm ] | ||
Post subject: | RE:Insertion Sort For Words | ||
So my sort should look like this ?
|
Author: | Clayton [ Wed Feb 27, 2008 3:37 pm ] | ||
Post subject: | RE:Insertion Sort For Words | ||
One minor edit, see if you can spot it:
|
Author: | Civik [ Wed Feb 27, 2008 10:01 pm ] |
Post subject: | RE:Insertion Sort For Words |
Im confused, you added .artist but am i not supposed to sort the whole record not just artist? Also, the way i have my code currently, will that move the other parts of data to the same spot as well? like artist album and year get put in the right spot with the names as they are organized |
Author: | Tony [ Thu Feb 28, 2008 12:02 am ] |
Post subject: | RE:Insertion Sort For Words |
I think you need to step back and revisit the basics of records. what is a record? what does it mean that record_1 > record_2 ? what does it mean that record_1.artist > record_2.artist ? |