1

Please provide an example how to sort ArrayList?

    public static JSONArray sortJSONArrayAlphabetically(JSONArray jArray) throws JSONException {
    if (jArray != null) {
        // Sort:
        ArrayList<String> arrayForSorting = new ArrayList<>();
        for (int i = 0; i < jArray.length(); i++) {
            arrayForSorting.add(jArray.get(i).toString());
        }
        Collections.sort(arrayForSorting);

        // Prepare and send result:
        JSONArray resultArray = new JSONArray();
        for (int i = 0; i < arrayForSorting.size(); i++) {
            resultArray.put(new JSONObject(arrayForSorting.get(i)));
        }
        return resultArray;
    }
    return null; // Return error.
}

this method fail

warning: [options] bootstrap class path not set in conjunction with -source 1.6 1 warning Init env executing with cn1env = ${cn1env} for ../../CodenameOne/GUIBuilder/src Note: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details. Note: Some input files use unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Compile is forcing compliance to the supported API's/features for maximum device compatibility. This allows smaller code size and wider device support F:\data_kdp\programs\CodenameOne\ws_codenameone\forcesoscn1appclient_kdp\src\com\geotracksolutionsint\forcesos\utils\Utils.java:9006: error: no suitable method found for sort(ArrayList) Collections.sort(arrayForSorting); method Collections.<T#1>sort(List<T#1>) is not applicable (inference variable T#1 has incompatible bounds equality constraints: String upper bounds: Comparable<? super T#1>) method Collections.<T#2>sort(List<T#2>,Comparator<? super T#2>) is not applicable (cannot infer type-variable(s) T#2 (actual and formal argument lists differ in length)) where T#1,T#2 are type-variables: T#1 extends Comparable<? super T#1> declared in method <T#1>sort(List<T#1>) T#2 extends Object declared in method <T#2>sort(List<T#2>,Comparator<? super T#2>) Note: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details. Note: Some input files use unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.

Duran k
  • 91
  • 6
  • The code should work. It seems to be a problem with your project configuration which I assume is pretty old. Which IDE are you using? Did you migrate to maven? – Shai Almog Apr 10 '21 at 04:13
  • I 'm using netbeans 8.2. I have not migrate to maven yet. – Duran k Apr 12 '21 at 14:25
  • could you tell me why this error occur and how to solve ? – Duran k Apr 12 '21 at 14:27
  • Looking at the code again try changing: `ArrayList arrayForSorting = new ArrayList<>();` to `List arrayForSorting = new ArrayList<>();`. Both should work. Also I would suggest using `Collections.sort(arrayForSorting, new CaseInsensitiveOrder());` – Shai Almog Apr 13 '21 at 01:38

0 Answers0