Angular2 array sorting function is not functioning properly on mobile browsers

I'm currently working with a code snippet that looks like this:

const words = ['apple', 'banana', 'zoom']

words.sort((first, second) => {
  const a = first;
  const b = second;

  return a == b ? 0 : a < b || a == null ? -1 : 1;

While this code functions properly in desktop Chrome browsers, I've noticed that it sorts the opposite way in mobile Chrome browsers.

Answer №1

Try this method

Ascending order

 const animals = ["Dog", "Cat", "Lion", "Elephant"];
animals.sort();
console.log(animals)

Descending order

const animals = ["Dog", "Cat", "Lion", "Elephant"];
animals.sort();
animals.reverse();
console.log(animals)

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

ejs-lineargauge major divisions and minor divisions centered

I've been trying to align majorTicks and minorTicks in the middle of the axis, but so far I haven't found a solution despite searching and googling extensively. Here's a snippet of my code: majorTicks: { height: 12, interval: 1, width ...

Observe the classList object of the material element to obtain its properties

I am currently using the mat-autocomplete feature and I am trying to remove focus from the input after selecting an element without needing a click. The mat-focused class within the mat-form-field is responsible for focusing on the mat-auto-complete. By re ...

Search timeout restriction

I have a function that makes a request to the server to retrieve data. Here is the code for it: export default class StatusChecker { constructor() { if (gon.search && gon.search.searched) { this.final_load(); } else { this.make_req ...

Using react-hook-form to easily update form data

While working on my project with react-hook-form for updating and creating details, I encountered a problem specifically in the update form. The values were not updating properly as expected. The issue seems to be within the file countryupdate.tsx. import ...

The functionality of crypto.randomBytes is not supported in Angular

I am utilizing the ethereumjs-wallet library in angular4, import EthereumWallet from 'ethereumjs-wallet'; var wallet = EthereumWallet.generate(); const jsV3 = wallet.toV3(passwd); the code snippet inside the library is displayed below var pri ...

Having difficulty using the Angular 6 "generic type elementref requires 2 type arguments" error message in my code

When attempting to use ElementRef, I included the following import statement: import { Component, OnInit, ElementRef, ViewChild } from '@angular/core'; I followed the examples and injected it into the constructor as shown: constructor(private ...

Reactify TypeScript: Accurate typings for onChange event

How can I resolve the issues with types for target: { value: any, name: any }? The errors I encounter include Duplicate identifier 'any'. and Binding element 'any' implicitly has an 'any' type.. Additionally, why does the erro ...

What are some ways to customize the appearance of gridlines in a PrimeNG p-table?

Currently, I am attempting to eliminate or modify the color of the gridlines on my p-table. I have gone through the code in the browser to locate the specific element responsible for these lines, but unfortunately, it has eluded me thus far. Even after t ...

What is the best way to provide a parameter to the query function in an Angular service class?

This snippet of code was generated by jHipster. invoice.component.ts @Component({ selector: 'jhi-invoice', templateUrl: './invoice.component.html' }) export class InvoiceComponent implements OnInit, OnDestroy { loadAll() ...

The status property in Angular 4 is currently not defined or is referencing a null value

I am currently working with angular 4 and ionic 3, and encountering an error message that says unable to get property 'status' undefined or null reference from the JSON data I am displaying on my HTML page. Here is a snippet of my home.ts file: ...

Send all Apache requests within a specified URL directory to a particular file, while directing all other requests to a different file

I am in the process of setting up an Angular application along with a basic PHP CRUD API backend on my Raspberry Pi 3 using Apache. My goal is to configure the mod_rewrite rules to first check for existing files or directories, then redirect requests for t ...

Angular 5 - Jasmine Tests explained: Encounter with the puzzling error message: "Error: Provider for the NgModule 'DynamicTestModule' is invalid, as only instances of Provider and Type are permitted"

I'm having trouble running tests on a component class. Here's the error message from the stack: Error: Invalid provider for the NgModule 'DynamicTestModule' - only instances of Provider and Type are allowed, got: [AlertModaldataCompon ...

How can Angular be used to create core styles in a CSS file rather than adding them dynamically within style tags?

Whenever I import a component, like MatTabsModule, it automatically generates styles within the head section: <style>.mdc-tab{min-width:90px;padding-right:24px....</style> I'd prefer to have all these styles generated in an external CSS f ...

Error message: "The property is not found within the specified type when using the OR operator with

Within my Angular component, I am faced with a challenge involving an Input that can be one of two types. @Input() profile: UserProfileDetails | BusinessProfileDetails; The structure of the profile template is straightforward and I want to avoid duplicati ...

Waiting for nested observables to complete in Angular 2

Before proceeding to another page in my Angular app, I need two nested Observables to complete. However, I am facing synchronization issues as they are nested within each other. These Observables are initialized in my authentication service. authentication ...

Detecting incorrect serialized data entries based on data types

In the scenario where the type MyRequest specifies the requirement of the ID attribute, the function process is still capable of defining a variable of type MyRequest even in the absence of the ID attribute: export type MyRequest = { ID: string, ...

Issue with Ionic 2's infinite scroll not triggering method on second scroll attempt

I utilized the ion-tab element to showcase a page (inboxitem) which encompasses ion-list and makes use of ion-infinite-scroll. The following code snippet resides in inboxitem.html <ion-content class="inbox can-swipe-list"> <ion-list> ...

How can you set a value to a radio button with Angular 4?

How can I set the JSON value for the radio buttons (Unlimited, Custom) below? I tried using [(ngModel)]="user.accessSchedule.fullAccess", but it's not working. <ol-radio-group [selected]="unlimitedAccessSchedule" id="accessSchedule" [(ngModel)]=" ...

Guide to setting up a universal error handling system compatible with forkJoin in Angular 6

I recently implemented error handling in my app following the guidelines from the official Angular 6 tutorial in rest.service.ts ... return this.http.get(url).pipe(catchError(this.handleError)); } private handleError(error: HttpErrorResponse) ...

Issue encountered in Loopback 4: Error with @repository dependency injection (TypeError: Undefined property 'findOne')

As I set up Bearer Token authentication for my Loopback 4 application, I referenced this implementation guide: https://github.com/strongloop/loopback-next/tree/master/packages/authentication. Within my src/providers/auth-strategy.provider.ts file, I encou ...