5

我有一个ArrayList<String>Java。现在我想根据一些要求对其进行排序。

例如,我在 ArrayList 中有这些项目:

xyz
bcd
abc_locked
cde
efg_locked
fgh

我想推回最后的那些_locked并保持顺序,使这个:

xyz
bcd
cde
fgh
abc_locked
efg_locked

做这个的最好方式是什么?我是否必须遍历列表删除字符串并再次添加它?或者,还有更好的方法?

4

5 回答 5

5

您可以尝试使用此比较器:

Comparator<String> comparator = new Comparator<String>() {

     @Override
     public int compare(String arg1, String arg2) {

         if (arg1.matches("^.*_locked$") && arg2.matches("^.*_locked$")) {
             // Both string have _locked at the end. Retain the order.
             return 0;
         } else if (arg1.matches("^.*_locked$")) {
             // First string have _locked. Swap.
             return 1;
         } else if (arg2.matches("^.*_locked$")) {
             // Second string have _locked. No need to swap
             return -1;
         } 

         // None of the string have _locked. Retain the order
         return 0;

     }
};

Collections.sort(list, comparator);
于 2013-06-27T13:01:50.550 回答
1

使用比较器:

Collections.sort(list, new Comparator(){
    public int compare(String o1, String o2) {
        if ((o1.endsWith("_locked")&&(!o2.endsWith("_locked"))){
            return 1;
        }
        else if (!(o1.endsWith("_locked")&&(o2.endsWith("_locked"))){
            return 1;
        }
        else {
            //Fallback sorting based on start of string left as exercise to reader
        }
    }

});
于 2013-06-27T13:02:27.910 回答
1

您可以尝试使用匿名参数化比较器,如下所示:

ArrayList<String> myList = new ArrayList<String>();
myList.add("xyz");
myList.add("bcd");
myList.add("abc_locked");
myList.add("cde");
myList.add("efg_locked");
myList.add("fgh");
Collections.sort(myList, new Comparator<String>() {
    @Override
    public int compare(String arg0, String arg1) {
        if (!arg0.contains("_locked") && !arg1.contains("_locked")) {
            return arg0.compareTo(arg1);
        }
        else if (arg0.contains("_locked") && arg1.contains("_locked")) {
            return arg0.compareTo(arg1);
        }
        else if (arg0.contains("_locked")) {
            return 1;
        }
        else {
            return -1;
        }
    };
});
System.out.println(myList);

输出:

[bcd, cde, fgh, xyz, abc_locked, efg_locked]
于 2013-06-27T13:03:45.463 回答
1
Comparator<String> comparator = new Comparator<String>() {

 @Override
 public int compare(String arg1, String arg2) {

     if (arg1.endsWith("_locked") && arg2.endsWith("_locked")) {
         return 0;
     } else if (arg1.endsWith("_locked")) {
         return 1;
     } else if (arg2.endsWith("_locked")) {
         return -1;
     } 
     return 0;
 }
};
于 2013-06-27T13:24:54.690 回答
-1

你可以试试这个

Collections.sort(list, new Comparator() {
            @Override
            public int compare(String s1, String s2) {

                // ascending order
                 return id1.compareTo(id2);

                // descending order
                //return id2.compareTo(id1);
            }
        });

对于对象集合

/*
  Here myItems is an arraylist MyItem added randomly 
  MyItem got a property int id 
  This method provide me myItems in ascending order of id's
*/
Collections.sort(myItems, new Comparator<MyItem>() {

            @Override
            public int compare(MyItem lhs, MyItem rhs) {
                // TODO Auto-generated method stub

                int lhsId = lhs.getId();
                int rhsId = rhs.getId();

                return lhsId>rhsId ? 1 : -1;
            }
        }); 

当然你可以参考这个

于 2013-06-27T13:04:48.713 回答