我有此代码,但仅适用于小写字母。我希望它在不考虑大写字母的情况下对列表进行排序。
package sortarray.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; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String fruits[] = new String[7];// Sorted array fruits[0] = "apple"; fruits[1] = "apricot"; fruits[2] = "banana"; fruits[3] = "mango"; fruits[4] = "melon"; fruits[5] = "pineapple"; fruits[6] = "peach"; char currChar = fruits[0].charAt(0);// Get first char 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("Comparing ", fruits[i].charAt(0) + "," + currChar); if (fruits[i].charAt(0) == currChar) { if (match == false)// new match? { match = true;// Reset search tmp.clear();// clear existing items tmp.add(fruits[i - 1]); Log.d("Started 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.clear();// clear existing items } currChar = fruits[i].charAt(0); } tmp.add(fruits[len - 1]); matchedFruits.add(tmp.toArray(new String[tmp.size()]));// add left over // 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); } } }
这是最佳方法的简单Java示例:
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Sorter { String fruits[] = new String[7]; List<String> lst; Sorter() { lst = new ArrayList<String>(); // initialise UNSORTED array 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[] args) { 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 Comparator<Object> { public int compare(Object o1, Object o2) { String s1 = (String) o1; String s2 = (String) o2; return s1.toLowerCase().compareTo(s2.toLowerCase()); } } }