Sorting Items While Ignoring Articles
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 - 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('');
Developer | Dan |
Username | danbuda |
Uploaded | January 07, 2023 |
Rating | 3 |
Size | 3,002 Kb |
Views | 6,072 |
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!
Name | Size |
Just Some Buttons | 1,568 Kb |
Sass Mixin Example | 1,944 Kb |
Sticky Footer | 1,655 Kb |
React Routing with this.props.children | 2,725 Kb |
JavaScript Alphabetize Function | 2,596 Kb |
Simple Grid | 1,960 Kb |
Alphabetizing Lists with JavaScript | 2,719 Kb |
Simple React Router | 2,707 Kb |
My First Mixin | 2,017 Kb |
CSS Grid Using Inline-Block | 1,800 Kb |
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!
Name | Username | Size |
IE11 Test | Boostnewmedia | 4,998 Kb |
Simple Weather App | Cmwebby | 0 Kb |
Gulpfile | Aimhigherwebdesign-amy | 1,765 Kb |
Loading animation with css | Icebob | 2,947 Kb |
Review test | Otro_user_gil | 4,054 Kb |
Right Click Menu | Anodpixels | 2,252 Kb |
Mini Profile | Frytyler | 3,828 Kb |
Resume | Rottingroom | 5,483 Kb |
Minimelephant | Smashlee | 2,507 Kb |
Sort the Knowlege | Eprouver | 3,915 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!