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 ?
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.
There are two ways to sort a string array in Java: Using User-Defined Logic. Using the Arrays. sort() Methodm.
localeCompare() enables case-insensitive sorting for an array.
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.
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:
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:
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. ...
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"]
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With