【问题标题】:String sort on basis of length and then on basis of case字符串先按长度排序,再按大小写
【发布时间】:2019-02-19 14:54:34
【问题描述】:

我正在尝试根据长度和大小写敏感度对字符串列表进行排序。

示例: 排序前:[a, abc, b, fe, e, ABC, Abc]

排序后:[a, b, e, fe, abc, Abc, ABc, ABC]

public static void main(String[] args) {

    List<String> stringList = new ArrayList<>();
    stringList.add("a");
    stringList.add("abc");
    stringList.add("b");
    stringList.add("fe");
    stringList.add("e");
    stringList.add("ABC");
    stringList.add("Abc");
    stringList.add("ABc");

    System.out.print("Before Sort:");
    System.out.println(stringList);

    Collections.sort(stringList, new Comparator<String>(){
        @Override
        public int compare(String o1, String o2) {
                if(o1.length() > o2.length())
                {
                    return 1;
                }
                else if(o1.length() < o2.length()){
                    return -1;
                }
                else if(o1.length() == o2.length()){
                    return return o1.compareTo(o2);
                }
                else return 0;
        }
    });
    System.out.print("After Sort :");
    System.out.println(stringList);
}

以上代码根据长度对列表进行排序,但未能根据大小写敏感度排序。

它给出了输出, [a, b, e, fe, ABC, ABc, Abc, abc]

预期输出 排序后:[a, b, e, fe, abc, Abc, ABc, ABC]

感谢任何帮助。

【问题讨论】:

    标签: java string


    【解决方案1】:

    我认为您需要考虑更多情况,例如AFefE。您的排序似乎没有自然的流程。下面的代码得到你想要的输出。

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    /**
     *
     * @author blj0011
     */
    public class JavaApplication114
    {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)
        {
            // TODO code application logic here
            List<String> stringList = new ArrayList<>();
            stringList.add("a");
            stringList.add("abc");
            stringList.add("b");
            stringList.add("fe");
            stringList.add("e");
            stringList.add("ABC");
            stringList.add("Abc");
            stringList.add("ABc");
    
            System.out.print("Before Sort:");
            System.out.println(stringList);
    
            Collections.sort(stringList, new Comparator<String>()
            {
                @Override
                public int compare(String o1, String o2)
                {
                    if (o1.length() > o2.length()) {
                        return 1;
                    }
                    else if (o1.length() < o2.length()) {
                        return -1;
                    }
                    else if (o1.length() == o2.length()) {
                        if (o1.length() == 1) {
                            return o1.compareTo(o2);
                        }
                        else {
                            return o2.compareTo(o1);
                        }
    
                    }
                    else {
                        return 0;
                    }
                }
            });
            System.out.print("After Sort :");
            System.out.println(stringList);
        }
    }
    

    输出:

    run:
    Before Sort:[a, abc, b, fe, e, ABC, Abc, ABc]
    After Sort :[a, b, e, fe, abc, Abc, ABc, ABC]
    BUILD SUCCESSFUL (total time: 0 seconds)
    

    【讨论】:

      【解决方案2】:

      线

      return o1.toLowerCase().compareToIgnoreCase(o2.toLowerCase())
      

      与您的规格相矛盾。如果您想要区分大小写,则不应与忽略大小写进行比较并转换为小写。

      【讨论】:

        【解决方案3】:

        'a' > 'A' 按自然顺序排列。如果您想按降序对字符串进行排序,请将o1.compareTo(o2); 更改为o2.compareTo(o1); 作为旁注,您可以使用Integer.compare(int x, int y) 来消除重复的 if else。示例:

            Collections.sort(stringList, new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    int comp = Integer.compare(o1.length(), o2.length());
                    if (comp == 0) {
                        return o1.length()==1 ? o1.compareTo(o2):o2.compareTo(o1);
                    } 
                     return comp;
                }
            });
        

        【讨论】:

        • @slider_rahul 错过了长度 = 1 的字符串的自然顺序。已编辑。
        猜你喜欢
        • 2015-06-29
        • 1970-01-01
        • 2015-07-29
        • 1970-01-01
        • 2018-03-11
        • 2015-03-15
        • 2014-05-19
        • 1970-01-01
        • 2018-01-21
        相关资源
        最近更新 更多