在BlackBerry中对String数组进行排序

时间:2012-05-29 12:25:42

标签: blackberry blackberry-jde

我需要按照升序排序String数组,如下所示。

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

怎么做?我需要帮助。

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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();
    }
  }
}

答案 2 :(得分: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;
                   }
               }

    }