简体   繁体   English

如何在Android中按字母顺序对字符串数组进行排序?

[英]How to sort String array Alphabetically in android?

I know there are many solutions to this question, but I am still facing a error. 我知道这个问题有很多解决方案,但是我仍然遇到错误。 I have a string array, and the data to the array is inserted through webservice . 我有一个字符串数组,并且通过webservice将数据插入数组。 Below is my code. 下面是我的代码。

protected void onPostExecute(String result) {
            dialog.dismiss();
            int dip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                    (float) 1, getResources().getDisplayMetrics());

            JSONArray jarray;

            try {
                jarray= new JSONArray(result);
                    name= new String[jarray.length()];
for (int i=0;i<jarray.length();i++){

                    JSONObject jobj = jarray.getJSONObject(i);
                name[i]=    jobj.getString("Name");
}

Now in name[i] I get the names ,but those are not arranged alphabetically. 现在以名称[i]我得到了名称,但是这些名称不是按字母顺序排列的。 I tried Arrays.sort(name); 我尝试了Arrays.sort(name); but it is giving me null pointer. 但这给了我空指针。 在此处输入图片说明

For alphabetic comparation you can use method compareTo(String) . 对于字母比较,可以使用compareTo(String)方法。 sorting mechnism you will have to develop yourself or use ArrayList an Collections.sort() method. 排序机制,您必须自己开发或使用ArrayList和Collections.sort()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM