Sorting Items While Ignoring Articles

Developer
Size
3,002 Kb
Views
6,072

How do I make an sorting items while ignoring articles?

Use JavaScript to sort a list (movies in this case) alphabetically while ignoring articles at the beginning of the movie title (a, an, the). . What is a sorting items while ignoring articles? How do you make a sorting items while ignoring articles? This script and codes were developed by Dan on 07 January 2023, Saturday.

Sorting Items While Ignoring Articles Previews

Sorting Items While Ignoring Articles - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Sorting Items While Ignoring Articles</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <ul id="list"></ul> <script src="js/index.js"></script>
</body>
</html>

Sorting Items While Ignoring Articles - Script Codes CSS Codes

#list { list-style: none; font-family: sans-serif;
}

Sorting Items While Ignoring Articles - Script Codes JS Codes

"use strict";
// The weirdest combination of random movies ever:
var movies = ["The Sound of Music", "A Beautiful Mind", "Ghostbusters", "An American Werewolf in London", "Tommy Boy", "Full Metal Jacket"];
// This function takes in a string as an argument - a movie in this example, and removes any article - a, an, the - and also uses the trim method to remove any whitespace left after doing so
function removeArticle(movie) { return movie.replace(/^(a |an |the )/i, '').trim();
}
// Sorts the list of movies using the function above to sort them based on the name of the movie without any article at the beginning
var sortedMovies = movies.sort(function (a, b) { return removeArticle(a) > removeArticle(b) ? 1 : -1;
});
// Renders the sorted list to the screen inside our ul element, mapping over each movie in the sorted array, putting li tags around the movie name, and removing the commas in between movies with the join method
document.querySelector('#list').innerHTML = sortedMovies.map(function (movie) { return "<li>" + movie + "</li>";
}).join('');
Sorting Items While Ignoring Articles - Script Codes
Sorting Items While Ignoring Articles - Script Codes
Home Page Home
Developer Dan
Username danbuda
Uploaded January 07, 2023
Rating 3
Size 3,002 Kb
Views 6,072
Do you need developer help for Sorting Items While Ignoring Articles?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Dan (danbuda) Script Codes
Create amazing web content with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!