Arranging elements in an array by the initial letter of each string using Javascript

I am facing an issue with comparing an array of strings, specifically [ 'a', 'aa', 'aaa' ]. When I use the sort() method, the order changes to ['aaa', 'aa', 'a']. However, I would like to compare these strings based on the characters at index 0 only so that the original input remains unchanged after sorting. I understand why the current order is ['aaa', 'aa', 'a'], but I need a way to sort them based solely on the characters at index 0.

If anyone has a solution, please share. Thank you.

Answer №1

As stated by @Nick Parsons in the comment section:

let array=["apple","banana","orange","grape","pear"];

console.log(array.sort((a,b)=>a[0].localeCompare(b[0])))

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

Is there a method to identify the Javascript responsible for altering a CSS property of an element?

I have a div that is toggled between hidden and visible states by clicking on another element. I can observe the changing visibility CSS property of the div using Firebug. The div is initialized using the Microsoft JavaScript library in code like this: Sys ...

"Enhance Your Website with Custom jQuery Preloaders

Currently, I am facing a challenge while developing a website. Specifically, I am struggling with resolving the issue of a blank page being displayed. The website that I am working on involves functionality where swiping right triggers data insertion into ...

Is there a way for me to receive the status code response from my backend server?

My component makes a call to a servlet, which then sends a POST HTTP request to the backend (using Spring Boot). I want the backend to return the status of the POST request that was sent earlier. This is my code: res= this.CS.postcompetenze(this.comp) Th ...

What is the best way to refresh data in a React component so that it displays the recently added data?

My website features a todo list that functions like this: Todo list Upon clicking the plus button, an input field appears allowing users to add items. Although the item is successfully added to the database, it does not immediately reflect on the webpage ...

The HTML5 thumbs-up feature will not be visible when not logged in

I have searched high and low on the web and even visited #facebook, but with no luck. I have experimented with the FBXML code as well, and it seems that if you are not logged into Facebook, the Like button will not appear on the page. The strange thing is ...

Troubleshoot: Why is my Google Chrome not playing videos? Uncover the solution with a

I have created a webpage with an HTML video tag that loads dynamically from a JSON file. I am currently using Chrome 50 for this project. The response is successful, and when inspecting the page using Chrome's developer tools, I can see the video tag ...

The onblur event is triggering prior to the value being updated

There are two input fields within a <form> element. I am trying to retrieve the value of one input field (dpFin) once it has been changed. The issue is that when I attempt to get the new value inside the event using var endDt = document.getElementByI ...

Async functions in Node.js are not executing in the expected sequence

I have a specific challenge of sending a POST request to my backend. This particular POST request necessitates the use of multipart/form-data. To facilitate this, I am in the process of converting image locations into Buffers before sending them all as par ...

Angular UI Bootstrap collapse directive fails to trigger expandDone() function

I am currently utilizing UI Bootstrap for Angular in one of my projects, and I have developed a directive that encapsulates the collapse functionality from UI Bootstrap. Here is how it looks: app.directive( 'arSection', ['$timeout', fu ...

Arranging an array of numeric values without utilizing the sort() function

Currently learning Javascript and I'm facing a challenge in an exercise from a tutorial, think it was learn street.com... The task is to sort an array of numbers without using the sort() method. Array looks like this: numbers =[12,10,15,11,14,13,16]; ...

Exploring the Display of TextBox When an Alternative is Chosen from DropDownList on ASP.NET Web Forms

I am working on a project where I have an asp:DropDownList, and the requirement is that when the user selects "Other" from the list, a hidden TextBox should be displayed to the user. Initially, the TextBox is supposed to be hidden. To hide the TextBox whe ...

Extracting array elements in MATLAB based on specific conditions

My matrix, A, is a 4x4. [1 2 3 4; 2 2 2 3; 5 5 5 5; 4 4 4 4] I can easily locate all values less than 4 in A (<4), but I am unsure how to create an 'if' statement that identifies rows with three or more values less than 4. For example, in ...

Difficulty in defining the impact of a function on a single menu

Q:: How can I restrict a jquery function to apply only on a specific set of list items in my website? $("li").mouseover(function(){ $(this).stop().animate({height:'230px'},{queue:false, duration:10, easing: 'easeOutBounce'}) ...

Sending an array with a specific identifier through JQuery ajax

I'm facing an issue where I am sending an array of values via jQuery AJAX to my servlet. However, the servlet is only picking up the first value in the array, even though there are more elements present. $.ajax({ type: "POST", url: "mySer ...

Is there a button that allows updating the text in a search field by entering multiple values one after the other?

Is it possible to insert multiple values sequentially into the search field using the same button? For example: value 1, value 2, value 3... Check out a demo here <form id="form1" name="form1" method="post"> <p> <input type=" ...

Transforming Material UI into a class-based component

How can I transform a React function into a class? I'm having trouble understanding how to adjust const { classes } = props; in a function to work within a class. You can find an example button function from Material UI here: import React from &apos ...

Tips for resolving the problem when encountering the "undefined data" issue in the success callback of an AJAX request

I am facing an issue with my JSP page where I have a button that calls an Ajax method named "sendmail()" upon click. The send mail API is in the Controller, and I am trying to display an alert message in the Ajax success function using data.message, but it ...

Converting a string to an array in Java: A step-by-step guide

Imagine having a string like this: String content = new String("[199, 56, 120]") The objective is to extract the numbers between the [] and commas and store them in an array. For example: array[0] = 199 array[1] = 56 array[2] = 120 Is there a way to ...

Best practices for refreshing the HTML5 offline application cache

My website utilizes offline caching, and I have set up the following event handler to manage updates: applicationCache.addEventListener('updateready', function () { if (window.applicationCache.status == window.applicationCach ...

Struggling to Make Div Refresh with jQuery/JS in my Rails Application

I'm currently facing an issue in my Rails app where I am unable to refresh a specific Div element. The Div in question is located in bedsheet_lines_index.html.erb <div id="end_time_partial" class="end_time_partial"> <%= render :partial ...