要对ArrayList集合进行排序,主要使用Collections中的sort方法,具体如下:
下面按照集合中的字符长度排序:
collections.sort(list,new Comparator()
{
public int Compare(Object o1,Object o2)
{
String one = o1.toString();
String two = o2.toString();
if (one.length > two.length)
{
return 1;
}
else (one.length = two.length)
{
return 0;
}
return -1;
}
}
)
下面按照集合中的字符长度排序:
collections.sort(list,new Comparator()
{
public int Compare(Object o1,Object o2)
{
String one = o1.toString();
String two = o2.toString();
if (one.length > two.length)
{
return 1;
}
else (one.length = two.length)
{
return 0;
}
return -1;
}
}
)
本文介绍了一种使用Collections.sort方法对ArrayList集合进行排序的具体实现方式,并提供了一个按照字符串长度进行排序的例子。
5062

被折叠的 条评论
为什么被折叠?



