Skip to content

Make brute force song-sorting more efficient  #1

@dmullen17

Description

@dmullen17

The function that matches the song bank to their respective seasonal playlists uses a brute-force nested for-loop method. This could be more efficient by shifting off a playlist once the first that isn't sorted into it is detected. This would work because the songs are chronologically sorted - so once a song can't be matched to a playlist then none of the others will either.

createPlaylists() {
        const playlistArray = this.createResponseObject(this.state.songs[0].added_at,
                                                  this.state.songs[this.state.songs.length - 1].added_at);
        const songs = this.state.songs; 
        songs.forEach(song => {
            const songDate = new Date(song.added_at);
            const [songYear, songMonth] = [songDate.getFullYear(), songDate.getUTCMonth()];
            
            playlistArray.forEach(playlist => {
                if (songYear === playlist.year && playlist.months.includes(songMonth)) {
                    playlist.songs.push(song);
                }
            });
        });
        this.setState({playlists: playlistArray});
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions