Skip to main content
March 23, 2026Noble Desktop/3 min read

Sortable Movies in JavaScript

Building Dynamic Movie Database Interfaces with JavaScript

Tutorial Series Overview

This is part one of a multi-part series covering how to build a complete sortable movie database display in JavaScript, progressing from data formatting to rendering and sorting functionality.

Key Features We'll Build

Time Format Conversion

Transform raw minutes into user-friendly hour and minute display format. Convert 134 minutes to 2h14m format for better readability.

Dynamic Sorting

Implement multi-criteria sorting capabilities for movie collections. Users can sort by name, duration, or release year.

Database Rendering

Display movie data in an organized, interactive interface. Present information in a clean, accessible format for users.

Video Transcription

Hi, I'm Brian McLain, an instructor of JavaScript and Python programming at Noble Desktop in New York City.

In this video series, we're building a sophisticated movie database display that demonstrates practical data manipulation techniques every developer should master. Our project starts with a dataset of movies that loads into the browser with full sorting capabilities—alphabetically by title, chronologically by release year, and by runtime duration. This type of dynamic data presentation remains essential for modern web applications, from e-commerce platforms to content management systems.

Today's focus addresses a common real-world challenge: transforming raw data into user-friendly formats. Consider how IMDb displays Black Panther's runtime as "2h 14m" rather than "134 minutes." This human-readable format significantly improves user experience, and implementing this transformation teaches us valuable lessons about data processing and object manipulation in JavaScript. Our movie dataset contains an array of objects, each with properties for name, year, and mins—but we need to enhance this structure to better serve our users.

Let's implement this data transformation step by step. We'll iterate through our movies array using a standard for loop: `let i = 0; i < movies.length; i++`. This approach gives us precise control over each iteration, which becomes crucial when modifying object properties in place.

For each movie object, we first extract the total minutes: `let mins = movies[i].mins`. Next, we calculate the hours by dividing the total minutes by 60 and rounding down using `Math.floor()`. This built-in JavaScript method ensures we get whole hours: `let hours = Math.floor(mins / 60)`. The Math.floor function is particularly useful here because it handles edge cases cleanly—a 119-minute movie correctly becomes 1 hour, not 2.

To find the remaining minutes, we employ the modulus operator (%), which returns the remainder after division. The expression `let remainderMin = mins % 60` gives us exactly what we need. For Black Panther's 134 minutes, this calculation yields 14 remaining minutes after accounting for the 2 full hours.

Now we can add our formatted property to each movie object: `movies[i].ourMin = \`${hours}h ${remainderMin}m\``. This template literal syntax creates clean, readable strings while maintaining the familiar IMDb format that users expect. The beauty of this approach is that we're enhancing our data structure without losing the original information—the raw minutes remain available for calculations or alternative displays.

Let's verify our implementation by logging the updated array to the console. As you can see, each movie object now contains our new `ourMin` property. I notice we had a small syntax error initially—placing the dollar sign inside the template literal brackets rather than outside—but that's quickly corrected. These minor bugs are common when working with template literals, and catching them early through console logging is a best practice that saves debugging time later.

With our data transformation complete, Black Panther now displays as "2h 14m"—exactly the professional format we wanted. This technique scales beautifully whether you're working with dozens of movies or thousands of database records.

In our next video, we'll tackle the rendering process, transforming this enhanced data into polished HTML elements. The final video in this series will implement our sorting functionality, demonstrating how proper data structure supports complex user interactions. Until then, I'm Brian McLain with Noble Desktop in New York City—thanks for coding along.

We don't want it to say 134 minutes. We want it to say 2h14m like IMDb does.
The importance of user-friendly data formatting in web applications

Time Conversion Implementation Process

1

Set up the loop

Initialize a for loop to iterate through the movies array using standard counter variables and length property

2

Extract minutes value

Retrieve the mins property from each movie object to get the raw minute duration for conversion

3

Calculate hours

Divide minutes by 60 and use Math.floor to round down to get the whole number of hours

4

Find remainder minutes

Use the modulus operator to get the remaining minutes after extracting full hours

5

Create formatted string

Add new ourMin property using template literals to format as 'Xh Ym' display format

Common Template Literal Mistake

Be careful with template literal syntax - the dollar sign must be outside the parentheses, not inside. Incorrect: `(${hours})` Correct: `${hours}`

Before vs After Data Format

FeatureOriginal FormatImproved Format
Black Panther Duration134 minutes2h14m
User ExperienceRaw numerical dataIntuitive time format
Display ConsistencyTechnical formatIndustry standard
Recommended: The formatted approach matches user expectations from platforms like IMDb

JavaScript Operators Used

Math.floor()

Rounds down to the nearest integer, essential for converting decimal hours to whole numbers. Takes any number and returns the largest integer less than or equal to it.

Modulus Operator (%)

Returns the remainder after division, perfect for extracting leftover minutes. Divides the first number by the second and gives you what's left over.

Template Literals

Allows embedded expressions in strings using backticks and ${} syntax. Provides clean string interpolation for formatted output like time displays.

Key Takeaways

1Data formatting is crucial for user experience - raw numbers should be converted to intuitive formats like hours and minutes
2The Math.floor() function is essential for converting decimal values to whole numbers when working with time calculations
3Modulus operator provides an elegant solution for extracting remainder values in mathematical operations
4Template literals offer clean syntax for string interpolation and formatted output in modern JavaScript
5Adding new properties to existing objects allows you to enhance data without modifying original structure
6Console logging is an effective debugging technique for verifying object property additions and transformations
7Following industry standards like IMDb's time format improves user familiarity and interface consistency
8Breaking complex functionality into multiple steps makes code more maintainable and easier to debug

RELATED ARTICLES