Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting a string array with uppercase first including accents

So I was wandering around the internet searching for some sorting function in js. Here is the problem. We have a string array like this :

['único', 'UNICO', 'árbol', 'ARBOL', 'cosas', 'COSAS', 'fútbol', 'FUTBOL']

and we want somthing like this (Uppercase first):

['ARBOL', 'COSAS', 'FUTBOL', 'UNICO', 'árbol', 'cosas', 'fútbol', 'único']

or like this (lowercase first):

['árbol', 'cosas', 'fútbol', 'único', 'ARBOL', 'COSAS', 'FUTBOL', 'UNICO']

The thing is : it's really easy to get this :

['ARBOL', 'COSAS', 'FUTBOL', 'UNICO', 'cosas', 'fútbol', 'árbol','único']

with the .sort(); function but we don't want the accentuated words at the end so we use the

.sort(function(a, b) {
  return a.localCompare(b);
});

but we end up with this ...

['ARBOL', 'árbol', 'COSAS', 'cosas', 'FUTBOL', 'fútbol', 'UNICO', 'único']

Do you guys have any idea on how to combine both ?

like image 226
Aaron C. Avatar asked May 22 '19 08:05

Aaron C.


People also ask

How do you arrange an array in alphabetical order?

The sort() sorts the elements of an array. The sort() overwrites the original array. The sort() sorts the elements as strings in alphabetical and ascending order.

How do you sort an array of strings?

There are two ways to sort a string array in Java: Using User-Defined Logic. Using the Arrays. sort() Methodm.

Does localeCompare ignore case?

localeCompare() enables case-insensitive sorting for an array.

How do you sort an array of strings alphabetically in Java?

Using the toCharArray() method Get the required string. Convert the given string to a character array using the toCharArray() method. Sort the obtained array using the sort() method of the Arrays class. Convert the sorted array to String by passing it to the constructor of the String array.

How to sort array of strings in JavaScript?

The first option to sort properly an array of strings, is to provide as comparator the localeCompare method of a string, this method returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order. For example:

How to sort string by lower case and upper case?

Approach: The idea is simple to store lower case characters and upper case characters in two different vectors and sort both of the vectors. Then use the sorted vectors to get the sorted string. Below is the implementation of the above approach:

How to sort an array in ascending order?

1 Using the Arrays.sort () Method. In Java, Arrays is the class defined in the java.util package that provides sort () method to sort an array in ascending order. 2 Sort String Array in Ascending Order or Alphabetical Order. ... 3 Sort String Array in Descending Order or Reverse Natural Order 4 Using the reverseOrder () Method. ...

How to sort an array of words with grave accent?

The accent grave (grave accent) à, è, ù ... In JavaScript for example, sorting an array of words is pretty easy for strings that doesn't contain such characters, for example: ['Bogotá', 'Bucaramanga', 'Cali', 'Santa Marta', 'Cartagena'].sort (); // This will sort as // ["Bogotá", "Bucaramanga", "Cali", "Cartagena", "Santa Marta"]


1 Answers

You can simply extend the sort function to prioritize uppercase characters in the beginning of strings:

const arr = ['ÁRBOL', 'único', 'UNICO', 'árbol', 'ARBOL', 'cosas', 'COSAS', 'fútbol', 'FUTBOL'];

function startsWithUppercase(str) {
    return str.substr(0, 1).match(/[A-Z\u00C0-\u00DC]/);
}

arr.sort(function(a, b) {
    if (startsWithUppercase(a) && !startsWithUppercase(b)) {
        return -1;
    } else if (startsWithUppercase(b) && !startsWithUppercase(a)) {
        return 1;
    }
    return a.localeCompare(b);
});

console.log(arr);
like image 123
fjc Avatar answered Sep 22 '22 23:09

fjc