Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

sort array of strings

/* names of illnesses are written in order of 
how patients have arrived. Let's sort them in alphabetical order. */


const diseases = [
    "Mysophobia",
    "Fear of missing out",
    "Erythrophobia"
];

diseases.sort(function(a, b) {
  /* let's convert the strings to lowercase
  to ensure the comparison works */ 
  a = a.toLowerCase();
  b = b.toLowerCase();

    if (a < b) return -1; // a will come before b
    if (b < a) return 1; // b will come before a

  return 0;
});

console.log(diseases);

/* ["Erythrophobia","Fear of missing out", "Mysophobia"] */ 
Comment

PREVIOUS NEXT
Code Example
Javascript :: requirejs example 
Javascript :: sub array javascript 
Javascript :: javascript multidimensional array 
Javascript :: anguler test submit form 
Javascript :: how to insert div around element in javascript 
Javascript :: import everything javascript 
Javascript :: javascript set max length of string 
Javascript :: arrays inside array of objects 
Javascript :: find union of arrays 
Javascript :: returned value by findOneAndUpdate 
Javascript :: Ternary Operator react 3 Conditions 
Javascript :: cancel an event in javascript 
Javascript :: convery array of objects to map using immutables js 
Javascript :: get bytes from string javascript 
Javascript :: vue function data update 
Javascript :: listing range in javascript 
Javascript :: recharts change scale 
Javascript :: tailwind dynamic classes 
Javascript :: heroku buildpacks with react 
Javascript :: mongodb mongoose update convert string to object 
Javascript :: javascript timestamp conversion 
Javascript :: compare between two arrays javascript 
Javascript :: js loop through form inputs 
Javascript :: filter an array 
Javascript :: es6 javascript 
Javascript :: detect if overflow javascript 
Javascript :: jquery function return 
Javascript :: comment in js 
Javascript :: making axios call with headers 
Javascript :: how to generate random text in vue js 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =