Sorting method of a name array

Asked

Viewed 561 times

0

I am trying to sort an array of names with this method, more when it has minuscule letter and Acentuo the names go to the end of the list, someone knows a method that sorts considering capital letters , minusculas , with accentuation ?

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;  
        }
  • To ignore the uppercase letters there is the compareToIgnoreCase.

  • and the accent you know?

  • In Java if you use an Arraylist you can sort with compareTo. http://www.tutorialspoint.com/java/java_using_comparator.htm

  • you can use the method itself sort() vector to do this.

  • 1

    Ilgner Stackoveflow is not like other forums. Here the questions are not marked as answered.

  • 1

    In addition, asked the question for array of names, and the right answer is with array of names, if you want to know with Arraylist it would be better to create a new question.

Show 1 more comment

1 answer

1


In java you can use an object of the type Collator to make the ordination. Follow an example of use:

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;
        }
    }
}

This way he will order according to our Portuguese.

  • I tried to use with my arraylist public Static Arraylist<Person> sorts(Collator, Arraylist<Person> contacts) { Person tmp; for (int i = 0; i < contacts.size(); i++) { for (int j = i + 1; j < contacts.size(); j++) { if (collator.compare(contacts.get(i).name, contacts.get(j).name) > 0) { tmp=new Person(); tmp = contacts.get(i); contacts.set(i, contacts.get(i); contacts.set(j, tmp); } } } Return contacts; } more when it comes to sorting it just by picking up a contact, you can see pq?

  • Still in trouble? Coming to college I take a look

  • I’m trying to see where I went wrong , this method there works perfectly , more when I switched to accept my Person Arraylist it’s filling everything with just a name

  • I already found where I erred kk , I exchanged a j by i ai was always taking the same name and putting in the place of the other , vlw helped a lot

  • All right, thanks :D

  • @Iigner, when used ArrayList the easiest and efficient way is by using the function sort(List<T> list) of the Collections class... in this way: Collections.sort(listaAOrdenar);

  • yes, I even put in the comment of your question, da uma olhada la :D

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.