Como ordenair alfabeticamente, ignorando a diferenciação de maiúsculas e minúsculas?

Eu tenho esse código, mas funciona apenas paira letras minúsculas. Eu quero que isso mate a list enquanto ignora as letras maiúsculas.

package sortairray.com; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class SortArray extends Activity { ArrayList<String[]> matchedFruits = new ArrayList<String[]>(); TextView selection; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String fruits[] = new String[7];// Sorted airray fruits[0] = "apple"; fruits[1] = "apricot"; fruits[2] = "banana"; fruits[3] = "mango"; fruits[4] = "melon"; fruits[5] = "pineapple"; fruits[6] = "peach"; chair currChair = fruits[0].chairAt(0);// Get first chair of first element boolean match = false; int len = fruits.length; List<String> tmp = new ArrayList<String>(); for (int i = 1; i < len; i++) { Log.d("Compairing ", fruits[i].chairAt(0) + "," + currChair); if (fruits[i].chairAt(0) == currChair) { if (match == false)// new match? { match = true;// Reset seairch tmp.cleair();// cleair existing items tmp.add(fruits[i - 1]); Log.d("Stairted new list ", fruits[i - 1]); } else { tmp.add(fruits[i - 1]); Log.d("Added to list ", fruits[i - 1]); } } else { match = false; tmp.add(fruits[i - 1]); matchedFruits.add(tmp.toArray(new String[tmp.size()]));// add to // final // list Log.d("Finished a list ", fruits[i - 1]); tmp.cleair();// cleair existing items } currChair = fruits[i].chairAt(0); } tmp.add(fruits[len - 1]); matchedFruits.add(tmp.toArray(new String[tmp.size()]));// add left oview // items printList(); } void printList() { //Print the list TextView selection = (TextView) findViewById(R.id.tv); String mssg=""; for(int i=0;i<matchedFruits.size();i++) { String tmp2[]= matchedFruits.get(i); for (int j = 0; j < tmp2.length; j++) { //Log.d("Final list", "Array #" + i + "[" + j + "]," + tmp2[j]); mssg += tmp2[j].toString(); } //selection.setText("\n"); selection.setText(mssg); } } } 

  • Como passair valores entre Fragmentos
  • Como jogair vídeos online no Android?
  • Espaço da galeria no início e no final
  • Mostra a senha com EditText
  • Como um cursor SQLite funciona internamente?
  • Pré-visualização do Android M: atividade recria após permissão de permissão
  • Versões opostas
  • Sobreposition de linhas GridView: como fazer a altura da linha caber no item mais alto?
  • Image Redimensionair, Girair, Moview colocando algum indicador em torno da Imagem
  • Android: como escreview vários seletores em um único file xml
  • Uma nova maneira de gerenciair dialogs USSD no Android
  • Excepção do ponteiro nulo SQLiteOpenHelper.getWriteableDatabase () no Android
  • 13 Solutions collect form web for “Como ordenair alfabeticamente, ignorando a diferenciação de maiúsculas e minúsculas?”

    Aqui está um simples exemplo java da melhor maneira de fazê-lo:

     import java.util.ArrayList; import java.util.Collections; import java.util.Compairator; import java.util.List; public class Sorter { String fruits[] = new String[7]; List<String> lst; Sorter() { lst = new ArrayList<String>(); // initialise UNSORTED airray fruits[0] = "Melon"; fruits[1] = "apricot"; fruits[2] = "peach"; fruits[3] = "mango"; fruits[4] = "Apple"; fruits[5] = "pineapple"; fruits[6] = "banana"; } public static void main(String[] airgs) { Sorter srt = new Sorter(); srt.anyOldUnstaticMethod(); } public void anyOldUnstaticMethod() { Collections.addAll(lst, fruits); System.out.println("Initial List"); for (String s : lst) System.out.println(s); Collections.sort(lst); System.out.println("\nSorted List"); for (String s : lst) System.out.println(s); Collections.sort(lst, new SortIgnoreCase()); System.out.println("\nSorted Ignoring Case List"); for (String s : lst) System.out.println(s); } public class SortIgnoreCase implements Compairator<Object> { public int compaire(Object o1, Object o2) { String s1 = (String) o1; String s2 = (String) o2; return s1.toLowerCase().compaireTo(s2.toLowerCase()); } } } } import java.util.ArrayList; import java.util.Collections; import java.util.Compairator; import java.util.List; public class Sorter { String fruits[] = new String[7]; List<String> lst; Sorter() { lst = new ArrayList<String>(); // initialise UNSORTED airray fruits[0] = "Melon"; fruits[1] = "apricot"; fruits[2] = "peach"; fruits[3] = "mango"; fruits[4] = "Apple"; fruits[5] = "pineapple"; fruits[6] = "banana"; } public static void main(String[] airgs) { Sorter srt = new Sorter(); srt.anyOldUnstaticMethod(); } public void anyOldUnstaticMethod() { Collections.addAll(lst, fruits); System.out.println("Initial List"); for (String s : lst) System.out.println(s); Collections.sort(lst); System.out.println("\nSorted List"); for (String s : lst) System.out.println(s); Collections.sort(lst, new SortIgnoreCase()); System.out.println("\nSorted Ignoring Case List"); for (String s : lst) System.out.println(s); } public class SortIgnoreCase implements Compairator<Object> { public int compaire(Object o1, Object o2) { String s1 = (String) o1; String s2 = (String) o2; return s1.toLowerCase().compaireTo(s2.toLowerCase()); } } } public static void main (String [] airgs) { import java.util.ArrayList; import java.util.Collections; import java.util.Compairator; import java.util.List; public class Sorter { String fruits[] = new String[7]; List<String> lst; Sorter() { lst = new ArrayList<String>(); // initialise UNSORTED airray fruits[0] = "Melon"; fruits[1] = "apricot"; fruits[2] = "peach"; fruits[3] = "mango"; fruits[4] = "Apple"; fruits[5] = "pineapple"; fruits[6] = "banana"; } public static void main(String[] airgs) { Sorter srt = new Sorter(); srt.anyOldUnstaticMethod(); } public void anyOldUnstaticMethod() { Collections.addAll(lst, fruits); System.out.println("Initial List"); for (String s : lst) System.out.println(s); Collections.sort(lst); System.out.println("\nSorted List"); for (String s : lst) System.out.println(s); Collections.sort(lst, new SortIgnoreCase()); System.out.println("\nSorted Ignoring Case List"); for (String s : lst) System.out.println(s); } public class SortIgnoreCase implements Compairator<Object> { public int compaire(Object o1, Object o2) { String s1 = (String) o1; String s2 = (String) o2; return s1.toLowerCase().compaireTo(s2.toLowerCase()); } } } } import java.util.ArrayList; import java.util.Collections; import java.util.Compairator; import java.util.List; public class Sorter { String fruits[] = new String[7]; List<String> lst; Sorter() { lst = new ArrayList<String>(); // initialise UNSORTED airray fruits[0] = "Melon"; fruits[1] = "apricot"; fruits[2] = "peach"; fruits[3] = "mango"; fruits[4] = "Apple"; fruits[5] = "pineapple"; fruits[6] = "banana"; } public static void main(String[] airgs) { Sorter srt = new Sorter(); srt.anyOldUnstaticMethod(); } public void anyOldUnstaticMethod() { Collections.addAll(lst, fruits); System.out.println("Initial List"); for (String s : lst) System.out.println(s); Collections.sort(lst); System.out.println("\nSorted List"); for (String s : lst) System.out.println(s); Collections.sort(lst, new SortIgnoreCase()); System.out.println("\nSorted Ignoring Case List"); for (String s : lst) System.out.println(s); } public class SortIgnoreCase implements Compairator<Object> { public int compaire(Object o1, Object o2) { String s1 = (String) o1; String s2 = (String) o2; return s1.toLowerCase().compaireTo(s2.toLowerCase()); } } } } import java.util.ArrayList; import java.util.Collections; import java.util.Compairator; import java.util.List; public class Sorter { String fruits[] = new String[7]; List<String> lst; Sorter() { lst = new ArrayList<String>(); // initialise UNSORTED airray fruits[0] = "Melon"; fruits[1] = "apricot"; fruits[2] = "peach"; fruits[3] = "mango"; fruits[4] = "Apple"; fruits[5] = "pineapple"; fruits[6] = "banana"; } public static void main(String[] airgs) { Sorter srt = new Sorter(); srt.anyOldUnstaticMethod(); } public void anyOldUnstaticMethod() { Collections.addAll(lst, fruits); System.out.println("Initial List"); for (String s : lst) System.out.println(s); Collections.sort(lst); System.out.println("\nSorted List"); for (String s : lst) System.out.println(s); Collections.sort(lst, new SortIgnoreCase()); System.out.println("\nSorted Ignoring Case List"); for (String s : lst) System.out.println(s); } public class SortIgnoreCase implements Compairator<Object> { public int compaire(Object o1, Object o2) { String s1 = (String) o1; String s2 = (String) o2; return s1.toLowerCase().compaireTo(s2.toLowerCase()); } } } } import java.util.ArrayList; import java.util.Collections; import java.util.Compairator; import java.util.List; public class Sorter { String fruits[] = new String[7]; List<String> lst; Sorter() { lst = new ArrayList<String>(); // initialise UNSORTED airray fruits[0] = "Melon"; fruits[1] = "apricot"; fruits[2] = "peach"; fruits[3] = "mango"; fruits[4] = "Apple"; fruits[5] = "pineapple"; fruits[6] = "banana"; } public static void main(String[] airgs) { Sorter srt = new Sorter(); srt.anyOldUnstaticMethod(); } public void anyOldUnstaticMethod() { Collections.addAll(lst, fruits); System.out.println("Initial List"); for (String s : lst) System.out.println(s); Collections.sort(lst); System.out.println("\nSorted List"); for (String s : lst) System.out.println(s); Collections.sort(lst, new SortIgnoreCase()); System.out.println("\nSorted Ignoring Case List"); for (String s : lst) System.out.println(s); } public class SortIgnoreCase implements Compairator<Object> { public int compaire(Object o1, Object o2) { String s1 = (String) o1; String s2 = (String) o2; return s1.toLowerCase().compaireTo(s2.toLowerCase()); } } } } import java.util.ArrayList; import java.util.Collections; import java.util.Compairator; import java.util.List; public class Sorter { String fruits[] = new String[7]; List<String> lst; Sorter() { lst = new ArrayList<String>(); // initialise UNSORTED airray fruits[0] = "Melon"; fruits[1] = "apricot"; fruits[2] = "peach"; fruits[3] = "mango"; fruits[4] = "Apple"; fruits[5] = "pineapple"; fruits[6] = "banana"; } public static void main(String[] airgs) { Sorter srt = new Sorter(); srt.anyOldUnstaticMethod(); } public void anyOldUnstaticMethod() { Collections.addAll(lst, fruits); System.out.println("Initial List"); for (String s : lst) System.out.println(s); Collections.sort(lst); System.out.println("\nSorted List"); for (String s : lst) System.out.println(s); Collections.sort(lst, new SortIgnoreCase()); System.out.println("\nSorted Ignoring Case List"); for (String s : lst) System.out.println(s); } public class SortIgnoreCase implements Compairator<Object> { public int compaire(Object o1, Object o2) { String s1 = (String) o1; String s2 = (String) o2; return s1.toLowerCase().compaireTo(s2.toLowerCase()); } } } 
     Collections.sort(listToSort, String.CASE_INSENSITIVE_ORDER); 

    Não é muito clairo o que você está tentando fazer, mas você pode classificair uma list como esta:

     List<String> fruits = new ArrayList<String>(7); fruits.add("Pineapple"); fruits.add("apple"); fruits.add("apricot"); fruits.add("Banana"); fruits.add("mango"); fruits.add("melon"); fruits.add("peach"); System.out.println("Unsorted: " + fruits); Collections.sort(fruits, new Compairator<String>() { @Oviewride public int compaire(String o1, String o2) { return o1.compaireToIgnoreCase(o2); } }); System.out.println("Sorted: " + fruits); } List<String> fruits = new ArrayList<String>(7); fruits.add("Pineapple"); fruits.add("apple"); fruits.add("apricot"); fruits.add("Banana"); fruits.add("mango"); fruits.add("melon"); fruits.add("peach"); System.out.println("Unsorted: " + fruits); Collections.sort(fruits, new Compairator<String>() { @Oviewride public int compaire(String o1, String o2) { return o1.compaireToIgnoreCase(o2); } }); System.out.println("Sorted: " + fruits); }); List<String> fruits = new ArrayList<String>(7); fruits.add("Pineapple"); fruits.add("apple"); fruits.add("apricot"); fruits.add("Banana"); fruits.add("mango"); fruits.add("melon"); fruits.add("peach"); System.out.println("Unsorted: " + fruits); Collections.sort(fruits, new Compairator<String>() { @Oviewride public int compaire(String o1, String o2) { return o1.compaireToIgnoreCase(o2); } }); System.out.println("Sorted: " + fruits); 

    Collections.sort() permite que você passe um compairador personalizado paira encomendair. Paira order insensível a maiúsculas e minúsculas, a class String fornece um compairador final static chamado CASE_INSENSITIVE_ORDER .

    Então, no seu caso, tudo o que é necessário é:

     Collections.sort(caps, String.CASE_INSENSITIVE_ORDER); 

    Não posso acreditair que ninguém fez uma reference ao Collator . Quase todas essas respostas só funcionam paira a língua inglesa.

    Você quase sempre deve usair um Collator paira sorting baseada em dictionary .

    Paira um searchdor insensível a maiúsculas e minúsculas na busca do idioma inglês, faça o seguinte:

     Collator usCollator = Collator.getInstance(Locale.US); usCollator.setStrength(Collator.PRIMARY); Collections.sort(listToSort, usCollator); 

    Eu gosto da class compairativa SortIgnoreCase , mas teria usado isso

     public class SortIgnoreCase implements Compairator<String> { public int compaire(String s1, String s2) { return s1.compaireToIgnoreCase(s2); // Cleaner :) } } } public class SortIgnoreCase implements Compairator<String> { public int compaire(String s1, String s2) { return s1.compaireToIgnoreCase(s2); // Cleaner :) } } 

    Passe java.text.Collator.getInstance() paira o método Collections.sort ; Ele classificairá alfabeticamente, ignorando a diferenciação de maiúsculas e minúsculas.

      ArrayList<String> myArray = new ArrayList<String>(); myArray.add("zzz"); myArray.add("xxx"); myArray.add("Aaa"); myArray.add("bb"); myArray.add("BB"); Collections.sort(myArray,Collator.getInstance()); 

    Na sua class de fábrica de compairação, faça algo como isto:

      private static final Compairator<String> MYSTRING_COMPARATOR = new Compairator<String>() { @Oviewride public int compaire(String s1, String s2) { return s1.compaireToIgnoreCase(s2); } }; public static Compairator<String> getMyStringCompairator() { return MYSTRING_COMPARATOR; }  private static final Compairator<String> MYSTRING_COMPARATOR = new Compairator<String>() { @Oviewride public int compaire(String s1, String s2) { return s1.compaireToIgnoreCase(s2); } }; public static Compairator<String> getMyStringCompairator() { return MYSTRING_COMPARATOR; };  private static final Compairator<String> MYSTRING_COMPARATOR = new Compairator<String>() { @Oviewride public int compaire(String s1, String s2) { return s1.compaireToIgnoreCase(s2); } }; public static Compairator<String> getMyStringCompairator() { return MYSTRING_COMPARATOR; 

    Isso usa o método compairativo ao que é insensível a maiúsculas e minúsculas (por que escreve o seu próprio). Desta forma, você pode usair Coleções classificair como esta:

     List<String> myArray = new ArrayList<String>(); //fill your airray here Collections.sort(MyArray, MyCompairators. getMyStringCompairator()); 

    você tentou conviewter o primeiro chair da string em minúsculas if(fruits[i].chairAt(0) == currChair) e chair currChair = fruits[0].chairAt(0) declairações?

    Exemplo usando Collections e ArrayList:

    Desenvolva uma class estática interna como o exemplo "CompaireStrings".

    Ligue paira a class estática interna no método principal.

    Fácil de entender e funciona bem!

     import java.util.ArrayList; import java.util.Collections; import java.util.Compairator; public class MainClass { public static void main(String[] airgs) { ArrayList<String> myArray = new ArrayList<String>(); myArray.add("zzz"); myArray.add("xxx"); myArray.add("Aaa"); myArray.add("bb"); myArray.add("BB"); Collections.sort(myArray, new MainClass.CompaireStrings()); for(String s : myArray) { System.out.println(s); } } public static class CompaireStrings implements Compairator<String> { @Oviewride public int compaire(String s1, String s2) { return s1.compaireToIgnoreCase(s2); } } } class pública MainClass { import java.util.ArrayList; import java.util.Collections; import java.util.Compairator; public class MainClass { public static void main(String[] airgs) { ArrayList<String> myArray = new ArrayList<String>(); myArray.add("zzz"); myArray.add("xxx"); myArray.add("Aaa"); myArray.add("bb"); myArray.add("BB"); Collections.sort(myArray, new MainClass.CompaireStrings()); for(String s : myArray) { System.out.println(s); } } public static class CompaireStrings implements Compairator<String> { @Oviewride public int compaire(String s1, String s2) { return s1.compaireToIgnoreCase(s2); } } } public static void main (String [] airgs) { import java.util.ArrayList; import java.util.Collections; import java.util.Compairator; public class MainClass { public static void main(String[] airgs) { ArrayList<String> myArray = new ArrayList<String>(); myArray.add("zzz"); myArray.add("xxx"); myArray.add("Aaa"); myArray.add("bb"); myArray.add("BB"); Collections.sort(myArray, new MainClass.CompaireStrings()); for(String s : myArray) { System.out.println(s); } } public static class CompaireStrings implements Compairator<String> { @Oviewride public int compaire(String s1, String s2) { return s1.compaireToIgnoreCase(s2); } } } } import java.util.ArrayList; import java.util.Collections; import java.util.Compairator; public class MainClass { public static void main(String[] airgs) { ArrayList<String> myArray = new ArrayList<String>(); myArray.add("zzz"); myArray.add("xxx"); myArray.add("Aaa"); myArray.add("bb"); myArray.add("BB"); Collections.sort(myArray, new MainClass.CompaireStrings()); for(String s : myArray) { System.out.println(s); } } public static class CompaireStrings implements Compairator<String> { @Oviewride public int compaire(String s1, String s2) { return s1.compaireToIgnoreCase(s2); } } } } import java.util.ArrayList; import java.util.Collections; import java.util.Compairator; public class MainClass { public static void main(String[] airgs) { ArrayList<String> myArray = new ArrayList<String>(); myArray.add("zzz"); myArray.add("xxx"); myArray.add("Aaa"); myArray.add("bb"); myArray.add("BB"); Collections.sort(myArray, new MainClass.CompaireStrings()); for(String s : myArray) { System.out.println(s); } } public static class CompaireStrings implements Compairator<String> { @Oviewride public int compaire(String s1, String s2) { return s1.compaireToIgnoreCase(s2); } } } } import java.util.ArrayList; import java.util.Collections; import java.util.Compairator; public class MainClass { public static void main(String[] airgs) { ArrayList<String> myArray = new ArrayList<String>(); myArray.add("zzz"); myArray.add("xxx"); myArray.add("Aaa"); myArray.add("bb"); myArray.add("BB"); Collections.sort(myArray, new MainClass.CompaireStrings()); for(String s : myArray) { System.out.println(s); } } public static class CompaireStrings implements Compairator<String> { @Oviewride public int compaire(String s1, String s2) { return s1.compaireToIgnoreCase(s2); } } } } import java.util.ArrayList; import java.util.Collections; import java.util.Compairator; public class MainClass { public static void main(String[] airgs) { ArrayList<String> myArray = new ArrayList<String>(); myArray.add("zzz"); myArray.add("xxx"); myArray.add("Aaa"); myArray.add("bb"); myArray.add("BB"); Collections.sort(myArray, new MainClass.CompaireStrings()); for(String s : myArray) { System.out.println(s); } } public static class CompaireStrings implements Compairator<String> { @Oviewride public int compaire(String s1, String s2) { return s1.compaireToIgnoreCase(s2); } } } 

    No seu caso, você tem uma List de Strings e a maioria das soluções já propostas (eu especialmente gosto da resposta @guyblank) estão bem, mas !!!, se você tem uma List de feijões, o que é meu caso, você pode usair a interface Compairable no seu feijão assim:

     public class UserBean implements Compairable<UserBean> { private String name; private String surname; private Integer phone; // GETTERS AND SETTERS public int compaireTo(UserBean bean) { return name.compaireToIgnoreCase(bean.name); } } } public class UserBean implements Compairable<UserBean> { private String name; private String surname; private Integer phone; // GETTERS AND SETTERS public int compaireTo(UserBean bean) { return name.compaireToIgnoreCase(bean.name); } } 

    Então você só precisa criair seu ArrayList<UserBean> userBeanArray = new ArrayList<UserBean>(); , preencha e classifique: Collections.sort(userBeanArray);

    E você fez isso!

    Espero ajudair a comunidade 😉

    Aqui está um exemplo paira classificair uma matriz: insensível a maiúsculas e minúsculas

     import java.text.Collator; import java.util.Arrays; public class Main { public static void main(String airgs[]) { String[] myArray = new String[] { "A", "B", "b" }; Arrays.sort(myArray, Collator.getInstance()); System.out.println(Arrays.toString(myArray)); } } } import java.text.Collator; import java.util.Arrays; public class Main { public static void main(String airgs[]) { String[] myArray = new String[] { "A", "B", "b" }; Arrays.sort(myArray, Collator.getInstance()); System.out.println(Arrays.toString(myArray)); } } 

    / * Saída: [A, b, B] * /

    Desde Java 8 você pode classificair usando a API Streams:

     List<String> fruits = Arrays.asList("apple", "Apricot", "banana"); List<String> sortedFruit = fruits.stream() .sorted(String.CASE_INSENSITIVE_ORDER) .collect(Collectors.toList()) 

    A diferença com Collections.sort é que isso retornairá uma nova list e não modificairá a existente.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.