【问题标题】:How to correctly order an ArrayList<String> containing Strings and numbers?如何正确排序包含字符串和数字的 ArrayList<String>?
【发布时间】:2014-06-20 14:30:47
【问题描述】:

我目前正在尝试根据“项目”后面的数字订购String 元素中的ArrayList(称为myarraylist)。这是myarraylist中的内容的sn-p:

Item 1
Item 2
Item 3
...
Item 9
Item 10

我想按以下顺序订购myarraylist

Item 10
Item 9
Item 8
...
Item 3
Item 2
Item 1

到目前为止,这是我尝试过的:

    Collections.sort(myarraylist, String.CASE_INSENSITIVE_ORDER);
    Collections.reverse(myarraylist);

但是,这会按以下顺序对myarraylist 进行排序

Item 9
Item 8
...
Item 3
Item 2
Item 10
Item 1

如您所见,Item 10 不合适,因为它的第一个数字“1”读取“10”。有谁知道如何以相反的数字顺序正确排序myarraylist

【问题讨论】:

标签: java android sorting arraylist


【解决方案1】:

这是因为默认字符串Comparator 使用lexicographical order——即逐个字符,就像在字典中一样。由于“1”在“2”之前,任何以“1”开头的字符串都将在任何其他以“2”开头的字符串之前。

您应该使用自定义比较器来实现Natural Sorting。一个很好的例子是来自 Dave Koelle 的 Alphanum,您可以这样使用:

Collections.sort(myarraylist, new AlphanumComparator());

【讨论】:

    【解决方案2】:

    我使用这个简单的类来订购我的Strings

    public abstract class StringOrderer {
    
        public static ArrayList<String> order(ArrayList<String> items, boolean ascending) {
    
            Collections.sort(items, new StringComparator());    
    
            // reverse the order
            if(!ascending) Collections.reverse(items);
    
            return items; 
        }
    
        class StringComparator implements Comparator<String> {
    
            @Override
            public int compare(String s1, String s2) {
    
                // use the users default locale to sort the strings
                Collator c = Collator.getInstance(Locale.getDefault());
    
                return c.compare(s1, s2);
            }
        }
    }
    

    基本思想是我有一个自定义 Comparator,它使用默认的Locale

    【讨论】:

      【解决方案3】:

      由于您在数组列表中使用字符串,它总是检查字符。最好尝试使用整数。它可能对你有用。

      【讨论】:

        【解决方案4】:

        由于String不能扩展,需要自定义比较时最好将ArrayList&lt;String&gt;改为ArrayList&lt;ClassWithStringAttribute&gt;,然后在ClassWithStringAttribute中实现Comparable。对于您的特定情况和说明,以下类应该可以工作,尽管不是一个好方法。

        myarraylist= StringSorter.getSortedList(myarraylist);
        

        会给你一个排序列表

        public class StringSorter implements Comparable<StringSorter>{
        
            String tempString;
            public StringSorter(String data) {
                this.tempString = data;
            }
            public static List<String> getSortedList(List<String> unsortedList){
                List<StringSorter> tempList=new ArrayList<StringSorter>();
                for (String current : unsortedList) {
                    tempList.add(new StringSorter(current));
                }
                Collections.sort(tempList);
                List<String> sortedString=new ArrayList<String>();
                for (StringSorter current : tempList) {
                    sortedString.add(current.tempString);
                }
                return  sortedString;
            }
        
            @Override
            public int compareTo(StringSorter other) {
                Integer otherInt=Integer.parseInt(other.tempString.replaceFirst("Item ", ""));
                Integer thisInt=Integer.parseInt(this.tempString.replaceFirst("Item ", ""));
                if(otherInt>thisInt){
                    return -1;
                }else if(otherInt<thisInt){
                    return 1;
                }else{
                    return 0;
                }       
            }   
        }
        

        【讨论】:

        • 这不是必需的。您可以为Collections.sort() 提供自定义Comparator
        【解决方案5】:

        这个自定义Comparator怎么样,

        public class StringNumSuffixComparator implements Comparator<String>{
        
            @Override
            public int compare(String o1, String o2) {
                String str1 = o1;
                String str2 = o2;
                Integer num1 = Integer.parseInt(str1.replaceAll("\\D+",""));
                Integer num2 = Integer.parseInt(str2.replaceAll("\\D+",""));        
                return num2.compareTo(num1);
            }
        }
        

        Collections.sort()对它们进行排序,

        Collections.sort(items, new StringNumSuffixComparator());
        

        【讨论】:

          猜你喜欢
          • 2017-02-24
          • 2017-06-15
          • 2020-12-12
          • 2014-05-05
          • 2019-12-27
          • 2013-11-03
          • 2019-03-28
          • 1970-01-01
          相关资源
          最近更新 更多