Arranging an array based on numerical values embedded in strings

My array is as follows:

var arr = [ '4msterdam', 'Par1s', 'N3w York', '2urich'];

Is there a way to sort the elements of this array based on the numbers included in each string?

Answer №1

One efficient method involves utilizing Array#sort combined with RegExp to sort an array based on the first digit found in each element.

var arr = ['4msterdam', 'Par1s', 'N3w York', '2urich'];
    sorted = arr.sort((a,b) => a.match(/\d/)[0] - b.match(/\d/)[0]);
    
    console.log(sorted);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adding an external JavaScript library file to a specific component in Angular 7 is a straightforward process. Let's walk through the

As a beginner in Angular framework, I've encountered an issue while working on creating a custom HTML template using Angular version 7. My template consists of various pages like home, about, product, etc. Specifically, on the home page, I am trying t ...

Navigate through an array of objects to retrieve particular key/value pairs

I have an item that has the following structure: [ { "title": "Job Title", "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="385d55595154785d55595154165b5755">[email p ...

Unexpectedly, the NodeJS application experiences a crash following numerous requests

I can't seem to figure out why my Nodejs app crashes after a certain number of requests (currently 22). The issue arises with this simple get request handler: router.get('/api/docs/fetch', async (req,res) => { try{ let docs = ...

Struggling to calculate the total of a property within an array of objects

I'm currently trying to calculate the sum of a specific property within an array object. Although I successfully accomplished this in one component, I am encountering difficulties replicating it in another. The error message being displayed is: this. ...

Encountering NotFoundHttpException with Jquery Ajax in Laravel 4

Hello everyone! I'm diving into the world of ajax and currently experimenting with sending form input to a controller through a route and then displaying it in a div. Unfortunately, I've hit a roadblock as I keep getting a NotFoundHttpException ...

Troubleshooting Error 400 while fetching JSON data using $http.get method in AngularJS

I keep encountering a 400 error when attempting to fetch the JSON data from the following URL using $http.get. $http.get('https://api.spotify.com/v1/search?q=artist:Owl+City+title:On+The+Wing&type=track&limit=1'). success(function(data) ...

Send information to the next route using Vue

Within my Vue frontend, there is a method called `moveToOrder` which asynchronously communicates with the backend to process a move from the cart collection to the orders collection: methods:{ async moveToOrder() { const res = await this.$axios.g ...

Enable a button or class to dynamically alter its color

Hi there! I'm new to coding and just started learning HTML and CSS. My question is: How can I change the color of buttons once they are clicked? <div class="icon-bar"> <a href="#"><i class="fa fa-search" ...

Adjust the fixed navbar position in MaterializeCSS as you scroll

First of all, I apologize for my limited proficiency in English. I have a website with a company logo at the top and a navigation bar below it. My goal is to change the position of the navigation bar to the top when scrolling past the company logo. I att ...

What is the method to obtain the content height of individual pages in Android, with or without the use of JavaScript?

I am facing an issue while trying to retrieve the content height of each webpage consecutively. When I load pages separately, I can successfully get the height of each page. However, when I attempt to fetch the content height continuously for webpages in a ...

Post a message utilizing javascript

Can a client-side tweet be generated using JavaScript, a text box, and a submit button? This involves entering the tweet text into the textbox, clicking the button, and then tweeting it with an authenticated account all within the client's browser. ...

Highlight all the written content within the text box

I'm struggling with a piece of code that is supposed to select all the text inside an input field: <input id="userName" class="form-control" type="text" name="enteredUserName" data-ng-show="vm.userNameDisplayed()" data-ng-model="vm.enteredUs ...

Creating an HTML string and then displaying its outer HTML in IE10 can be easily achieved. Just write the

My task is to write an HTML string to an element and then retrieve the resulting outer HTML from that element. This needs to be operational in IE10, latest versions of FF, Chrome, Safari, Android, iOS Safari but does not have to support any older browsers. ...

Reveal the class to the global scope in TypeScript

ClassExample.ts: export class ClassExample{ constructor(){} } index.html: <script src="ClassExample.js"></<script> // compiled to es5 <script> var classExample = new ClassExample(); //ClassExample is not defined < ...

What is the best way to determine the number of items in a class's property using the Swift programming language?

I need assistance with creating a method to calculate the total price of items stored in a property of a class. Programming is not my strong suit, so any help would be greatly valued. Thank you in advance for your help. ...

Adding new data to an object of objects can be a complex task, especially when the new data is coming from a state variable in React that is also an

I'm currently working on populating an object of objects with new data that is being stored in a state variable in React, which is also an object. Here's the code snippet I have: let userData = { harsh:{ password:"harsh", ema ...

Running a loop in jQuery to cycle through specified array sets: A step-by-step guide

After attempting to utilize jQuery & Arrays to construct a recurring logic, I have encountered difficulties with ensuring that the code reruns. My intention is for the code to proceed to the next array within the matrix whenever the user selects "Next Butt ...

Is it possible to display the content below the row of the clicked element when it is selected?

I am currently working on building a team page similar to the layout at My goal is to create a row of elements that float or display inline, with hidden content revealed beneath each selected element, pushing subsequent rows further down. Unfortunately, m ...

Toggle the visibility of a div based on the id found in JSON data

I am looking to implement a JavaScript snippet in my code that will show or hide a div based on the category ID returned by my JSON data. <div id="community-members-member-content-categories-container"> <div class="commun ...

`NodeJS and ExpressJS: A guide on accessing a remote phpMyAdmin server`

I am in the process of setting up a GraphQL server and facing an issue with connecting to a remote phpMyAdmin server provided by a friend. The error message I am encountering is: Error: getaddrinfo ENOTFOUND 54.193.13.37/phpmyadmin 54.193.13.37/phpmyadmi ...