我必须通过项目名称(第二个元素)对以下数组进行排序,我该怎么做?我被卡住了。
items[0] = new Product("001","Glue sticks",6,.06);
items[1] = new Product("002","Six inch rulers",25,.59);
items[2] = new Product("003","Paper-ream",5,6.99);
items[3] = new Product("004","Black ink pens",15,.97);
items[4] = new Product("005","No. 2 pencils",20,.30);
答案 0 :(得分:1)
使用比较器
Arrays.sort(items, new Comparator<Product>()
{
@Override public int compare(final Product p1, Product p2)
{
// TODO check for null object
return p1.getName().compareTo(p2.getName());
}
});
答案 1 :(得分:0)
使用arraylist和arraylist.sort()方法
可能更好