在Android的排序字符串数组列表 [英] sorting arraylist of string in android

查看:298
本文介绍了在Android的排序字符串数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串数组列表names'.which包含people.i的名字要排序的字母ArrayList中order.plz帮我

i am having a string arraylist 'names'.which contains names of people.i want to sort the arraylist in alphabetical order.plz help me

推荐答案

这将解决您的问题...

This will solve your problem...

ArrayList arrayList = new ArrayList();

//Add elements to Arraylist
arrayList.add("1");
arrayList.add("3");
arrayList.add("5");
arrayList.add("2");
arrayList.add("4");


Collections.sort(arrayList);

//display elements of ArrayList
System.out.println("ArrayList elements after sorting in ascending order : ");
for(int i=0; i<arrayList.size(); i++)
    System.out.println(arrayList.get(i));

要排序的ArrayList对象,使用 Col​​lection.sort 方法。这是一个 静态方法。它的排序ArrayList对象的元素融入升序排列。

To sort an ArrayList object, use Collection.sort method. This is a static method. It sorts an ArrayList object's elements into ascending order.


以防万一,若跌破code的评论不工作是指... 试试这个code ..


Just in case if the below code in comment doesnt work means... Try this code..

创建一个自定义比较器类:

import java.util.Comparator;

class IgnoreCaseComparator implements Comparator<String> {
  public int compare(String strA, String strB) {
    return strA.compareToIgnoreCase(strB);
  }
}

然后在你的排序:

Then on your sort:

IgnoreCaseComparator icc = new IgnoreCaseComparator();

java.util.Collections.sort(arrayList,icc);

这篇关于在Android的排序字符串数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆