Method of ordering a vector of names

0

I'm trying to sort a vector of names with this method, but when I have lowercase letters and accentuate the names go to the bottom of the list, does anyone know a method that orders considering capital letters, lowercase, with accent?

String nomes[] = { "Ana","aa","B" };
for (int i = 0; i < nomes.length - 1; ++i)  
    for (int j = i + 1; j < nomes.length; ++j)  
        if (nomes[i].compareTo(nomes[j]) > 0) {  
            String temp = nomes[i];  
            nomes[i] = nomes[j];  
            nomes[j] = temp;  
        }
    
asked by anonymous 18.11.2014 / 17:49

1 answer

1

In java you can use an object of type Collator to sort. Here is an example usage:

Collator brCollator = Collator.getInstance(new Locale("pt","BR"));
public static void sortStrings(Collator collator, String[] words) {
String tmp;
for (int i = 0; i < words.length; i++) {
    for (int j = i + 1; j < words.length; j++) { 
        if (collator.compare(words[i], words[j]) > 0) {
            tmp = words[i];
            words[i] = words[j];
            words[j] = tmp;
        }
    }
}

In this way he will sort according to our Portuguese.

    
18.11.2014 / 18:04