2012-05-29 90 views
1

我需要按升序对String(如下所示)进行排序。在BlackBerry中对字符串数组进行排序

String str[] = {"ASE", "LSM", "BSE", "LKCSE", "DFM"}; 

如何做到这一点?我需要帮助。

+2

,您是否试图使用[net.rim.device.api.util.Arrays.sort(对象[]一,比较器C)(HTTP: //www.blackberry.com/developers/docs/3.7api/net/rim/device/api/util/Arrays.html#sort(java.lang.Object [],net.rim.device.api.util.Comparator ))? –

回答

5

这个答案是基于SignareHeartBeat的建议。详情请参阅link。此链接,Sorting using java.util.Array可能会有所帮助。

// Initialization of String array 
String strs[] = {"One", "Two", "Threee", "Four", "Five", "Six", "Seven"}; 

// implementation of Comparator 
Comparator strComparator = new Comparator() { 
    public int compare(Object o1, Object o2) { 
     return o1.toString().compareTo(o2.toString()); 
    } 
}; 

// Sort 
Arrays.sort(strs, strComparator); 
+0

这是最简单也是最好的方法。 – rfsk2010

0

试试这个 -

import java.util.*; 
import java.io.*; 

public class TestSort1 { 

String [] words = { "Réal", "Real", "Raoul", "Rico" }; 

public static void main(String args[]) throws Exception { 
    try { 
    Writer w = getWriter(); 
    w.write("Before :\n"); 

    for (String s : words) { 
    w.write(s + " "); 
    } 

    java.util.Arrays.sort(words); 

    w.write("\nAfter :\n"); 
    for (String s : words) { 
    w.write(s + " "); 
    } 
    w.flush(); 
    w.close(); 
} 
catch(Exception e){ 
    e.printStackTrace(); 
} 

} 

// useful to output accentued characters to the console 
    public static Writer getWriter() throws UnsupportedEncodingException { 
    if (System.console() == null) { 
     Writer w = 
     new BufferedWriter 
     (new OutputStreamWriter(System.out, "Cp850")); 
     return w; 
    } 
    else { 
     return System.console().writer(); 
    } 
    } 
} 
+0

我可以在黑莓中使用“java.util.Arrays.sort(words)”吗? –

+0

我认为它会适用于最新的操作系统。 – Signare

+1

我使用的是JDE 4.2.1,但这不适用于我。 –

0

这里是我的解决方案: -

String str[]={"ASE","LSM","BSE","LKCSE","DFM"}; 

for(int j = 0; j < str.length; j++){ 
       for(int i = j + 1; i < str.length; i++) { 
        if(str[i].compareTo(str[j]) < 0) { 
         String t = str[j]; 
         str[j] = str[i]; 
         str[i] = t; 
        } 
       } 

    }